15th Apr 2025 11 minutes read Sales Growth Dataset Exploration – Using the Data Analyst Cheat Sheet on Real Sales Data Scott Davies data analysis SQL Project Table of Contents Overview How to Prepare the Data Finding the Overall Trends Sales Trends Determination Wrap-Up Have you ever used the Data Analyst Cheat Sheet to analyze a business dataset? In this article, you can see the commands from the cheat sheet in action. I’ll explore a real-world sales dataset to answer 10 key business questions using SQL. Following the guidance from the SQL for Data Analysis Cheat Sheet, I’ll demonstrate how SQL queries can help analyze trends, measure performance, and extract meaningful insights from sales data. To do this, we’ll use an open dataset from Tableau that contains sales data for home goods like desks and stationery. This dataset provides a practical way to explore business-related reporting questions and see how SQL can be used to uncover useful trends. Rather than a formal case study, this article focuses on mapping business questions to general SQL queries. You’ll see how SQL techniques align with real-world data analysis challenges and how different metric patterns emerge in reporting. By the end, you’ll be able to recognize these patterns and apply them to your own SQL reporting work. Let’s dive in! Overview This analysis is structured into two main parts. First, I examine overall trends, such as total sales, profit, and quantity sold. Then, I focus on sales trends, identifying patterns over time, by category, and across different business segments. To answer these questions, I use SQL techniques like aggregations, ratios, window functions, and subqueries. Common metric patterns, such as grouping data, tracking trends, calculating moving averages, and ranking results, help structure the analysis. The SQL for Data Analysis Cheat Sheet serves as a reference for key functions like GROUP BY, ranking, running totals, and date extractions. I added tags in the examples below to categorize them: brackets for SQL techniques [SQL], parenthesis for metric patterns (METRIC), and braces for cheatsheet references {SHEET}. These tags help organize the analysis for each example below. Examples are organized by difficulty level, starting with simple queries and gradually incorporating more advanced techniques. This makes it easy to follow along and apply similar approaches to other business datasets. How to Prepare the Data To start the analysis, we need to prepare the dataset. I use an open dataset from Tableau Public, which contains sales data for home goods like desks and stationery. Since it's widely used in business reporting, it’s a great resource for practicing SQL queries. Below are step-by-step instructions for downloading the dataset, importing it into a SQL environment, and making small adjustments to clean the data. Download the dataset from Tableau Public. Navigate to Learn → Sample Data → Business → Superstore Sales. Download the Superstore Sales dataset. Open the dataset in Excel. The relevant data is in the "Orders" tab. Open .xls file Go to ‘Orders’ tab in your spreadsheet SQL IDE download – optional for anyone who wants to follow along and recreate the metrics. I am using MySQL Workbench and MySQL Community Server. If you wish to use the same, the downloads are available below. I will show the steps for importing the dataset into MySQL Workbench. Download Links: MySQL Workbench: https://dev.mysql.com/downloads/workbench/ MySQL Community Server: https://dev.mysql.com/downloads/mysql/ I prefer to use MySQL Workbench with the Community Server, so the following steps are for setting it up: First, I confirm that the local instance connection is established. Then, I create a new schema by following these instructions: Click on the local instance connection. Choose Schemas from the top menu. Right-click and select Create Schema. Name the schema Tableau Superstore. Import the .xls file as a .csv under Tables using the Data Tables Import Wizard (convert .xls to .csv first). An alternative method is to import it as a JSON using a JSON converter (link to instructions, which is the method I used). Configure the import settings and choose the default field type for different source columns. Do not include the last blank row! Before the final step, make manual changes in the Alter Table: remove spaces from column names and replace them with underscores (_). The last step is to right-click and select Create Schema. Ta-da! Congratulations, the most complicated part is behind you. Name your Schema: Tableau Superstore should be fine. Use the Table Data Import Wizard to add the file, as shown in the screenshot. Upload the .csv file. Or, if converting to JSON, upload the JSON file instead. This requires using a .csv to .json converter, as shown in the picture. Keep all fields selected, but exclude the "blank" field and set all others as "text" fields. After this, the only thing left to do is right-click on the orders table and select Alter Table. The data preparation is now complete, and we’re ready to move on to the analysis. Pleasant bonus: you can always find all the requests and pieces of code from the article on the dedicated GitHub page. Finding the Overall Trends Let's warm-up on the easy level! To begin the analysis, we’ll start with some key performance indicators (KPIs) to get a high-level view of sales, profit, and quantity sold. These queries help summarize the dataset and provide a quick snapshot of overall business performance. KPI metrics: Total Sales, Profit, and Quantity Sold. Question: What is the total sales, profit, and quantity sold? [AGGREGATION] (OVERALL) {AGGREGATE FUNCTIONS} SELECT ROUND(SUM(Sales), 0) AS sales_total, ROUND(SUM(Profit), 0) AS profit_total, ROUND(SUM(Quantity), 0) AS quantity_total FROM `Tableau Superstore`.orders; Outcome: This query calculates the total sales, profit, and quantity sold across the entire dataset. It provides a big-picture view of overall business performance. KPI metrics: Monthly and Yearly Trends Question: What was the monthly/yearly sales, profit, and quantity sold? [AGGREGATION] (TREND) {AGGREGATE FUNCTIONS | EXTRACTING PARTS OF DATE} SELECT EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Year, EXTRACT(MONTH FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Month, ROUND(SUM(Sales), 0) AS sales_month, ROUND(SUM(Profit), 0) AS profit_month, ROUND(SUM(Quantity), 0) AS quantity_month FROM `Tableau Superstore`.orders GROUP BY 1, 2 ORDER BY 1, 2; Outcome: This query breaks down sales, profit, and quantity by year and month, helping us identify trends over time. It’s useful for spotting seasonal patterns or year-over-year growth. KPI metrics: Average Sales, Profit, and Quantity Sold Question: What was the average sales, profit, and quantity sold? [AGGREGATION] (OVERALL) {AGGREGATE FUNCTIONS} SELECT ROUND(AVG(Sales), 0) AS sales_average, ROUND(AVG(Profit), 0) AS profit_average, ROUND(AVG(Quantity), 0) AS quantity_average FROM `Tableau Superstore`.orders; Outcome: Instead of total values, this query calculates the average sales, profit, and quantity sold per order. It’s useful for understanding typical transaction size and comparing it to different time periods or business segments. Hope you managed to work through the first level of analysis! Now, let's take it a step further with more advanced queries. Medium level goes next, starting with a moving average calculation, which helps smooth out short-term fluctuations and identify sales trends over time. This approach allows us to forecast future sales by analyzing the patterns of previous months. Question: How can we forecast sales by month? [WINDOW FUNCTION] (TREND | MOVING AVERAGE) {AGGREGATE FUNCTIONS | EXTRACTING PARTS OF DATE | MOVING AVERAGE} SELECT EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Year, EXTRACT(MONTH FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Month, ROUND(SUM(Sales), 0) AS sales_total, ROUND(AVG(SUM(sales)) OVER(ORDER BY EXTRACT(MONTH FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 0) AS moving_average FROM `Tableau Superstore`.orders GROUP BY 1, 2 ORDER BY 1, 2; Outcome: This query calculates monthly sales totals and applies a moving average over the last three months (including the current one). This helps identify sales trends over time by smoothing short-term fluctuations, making it easier to forecast future performance. Hope you’re keeping up! Now, let’s tackle a more advanced query. Hard level goes next, where we calculate a cumulative sum of sales for 2017. This running total helps track sales progression month by month, giving a clear picture of overall performance throughout the year. Question: What is the cumulative sum of sales for 2017? Show by month. [WINDOW FUNCTION | CTE] (TREND | CUMULATIVE) {AGGREGATE FUNCTIONS | EXTRACTING PARTS OF DATE | RUNNING TOTAL | CTE} WITH monthly_report AS ( SELECT EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Year, EXTRACT(MONTH FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Month, ROUND(SUM(Sales), 0) AS monthly_sales_total FROM `Tableau Superstore`.orders WHERE EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) = 2017 GROUP BY 1, 2 ) SELECT order_month, yearly_sales_total, SUM(yearly_sales_total) OVER(ORDER BY order_Year) AS running_sales_total FROM monthly_report ORDER BY 1, 3 DESC; Outcome: This query first calculates monthly sales totals for 2017 using a Common Table Expression (CTE). Then, it applies a running total using a window function to sum sales progressively over the months. This helps analyze cumulative growth and detect sales trends within the year. Sales Trends Determination Let's start with the easy level again, but this time focusing on regional and category-based insights. These queries will help break down key metrics by different segments of the business, offering a clearer picture than just looking at overall totals. Question: What are the total sales, profit, and quantity for each region? [AGGREGATION] (GROUP) {AGGREGATE FUNCTIONS} SELECT Region, ROUND(SUM(Sales), 0) AS sales_total, ROUND(SUM(Profit), 0) AS profit_total, ROUND(SUM(Quantity), 0) AS quantity_total FROM `Tableau Superstore`.orders GROUP BY 1 ORDER BY 2 DESC; Outcome: This query groups sales, profit, and quantity by region, allowing us to compare performance across different geographical areas. It helps identify top-performing regions and those that may need further analysis. Question: What is the profit margin for each product category? [AGGREGATION | RATIO] (GROUP) {AGGREGATE FUNCTIONS | INTEGER DIVISION} SELECT Category, ROUND(SUM(Sales), 0) AS sales_total, ROUND(SUM(Profit), 0) AS profit_total, ROUND(SUM(Profit)/SUM(Sales), 2) AS profit_margin FROM `Tableau Superstore`.orders GROUP BY 1 ORDER BY 4 DESC; Outcome: This query calculates sales, profit, and profit margin for each product category. By dividing profit by sales, we get a profitability ratio, which helps determine which categories generate the highest returns. Let’s move on to the medium level, where we’ll analyze year-over-year (YoY) sales changes and calculate the percentage of total sales for each category. Question: Create a report showing sales, average sales, and Year-over-Year (YoY) change (delta) by year. [WINDOW FUNCTION] (TREND | DELTA) {AGGREGATE FUNCTIONS | EXTRACTING PARTS OF DATE | DIFFERENCE BETWEEN TWO ROWS (DELTA)} SELECT DISTINCT EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y'))) AS order_Year, ROUND(SUM(sales), 0) AS sales_total, ROUND(LAG(SUM(SALES)) OVER(ORDER BY EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y')))), 0) AS sales_prev_year, (ROUND(SUM(sales), 0)) - (ROUND(LAG(SUM(SALES)) OVER(ORDER BY EXTRACT(YEAR FROM DATE(STR_TO_DATE(order_date, '%m/%d/%Y')))), 0)) AS sales_yoy_difference FROM `Tableau Superstore`.orders GROUP BY 1 ORDER BY 1; Outcome: This query calculates total sales per year and compares it to the previous year using the LAG() window function. The result is a YoY difference, helping to track annual sales growth or decline at a glance. Question: What is the percentage of total sales for each category? [SUBQUERY] (GROUP | RATIO) {AGGREGATE FUNCTIONS | COMPUTING THE PERCENT OF TOTAL WITHIN A GROUP} SELECT DISTINCT category, ROUND(SUM(sales), 0) AS sales_total, ROUND((SUM(sales) / (SELECT SUM(sales) FROM `Tableau Superstore`.orders)) * 100, 0) AS percent_oftotal FROM `Tableau Superstore`.orders GROUP BY 1; Outcome: This query calculates each category’s sales as a percentage of total sales. The subquery ensures that the denominator remains fixed, allowing an accurate comparison of category performance relative to the entire dataset. We’ve reached the final part of the analysis, the hard level, so let’s take on one last challenge. This time, we’ll identify the top two best-selling sub-categories within each product category to see which items drive the most revenue. Question: What are the top two selling sub-categories within each group? [WINDOW FUNCTION | CTE] (RANK) {AGGREGATE FUNCTIONS | RANK | CTE} WITH category_ranking AS ( SELECT Category, Sub_Category, ROUND(SUM(Sales), 0) AS sales_total, DENSE_RANK() OVER(PARTITION BY Category ORDER BY SUM(SALES) DESC) AS sub_category_rank FROM `Tableau Superstore`.orders GROUP BY 1, 2 ) SELECT Category, Sub_Category, sales_total FROM category_ranking WHERE sub_category_rank <= 2 ORDER BY 1, 3 DESC; Outcome: This query ranks sub-categories within each category based on total sales using the DENSE_RANK() window function. Since we are grouping by category, we cannot use a simple aggregate function for ranking. Instead, the Common Table Expression (CTE) first calculates the rankings, and the final selection filters out only the top two sub-categories per category. Wrap-Up In this article, we explored 10 key SQL queries that help answer reporting-related business questions. We started with basic KPI metrics, moved on to trend analysis and forecasting, and finished with more advanced ranking and cumulative calculations. By working through these queries, you’ve seen common SQL patterns used in business reporting, including aggregations, window functions, and subqueries. Whether you followed along step by step or simply reviewed the examples, these techniques can serve as a practical reference for analyzing sales data in SQL. I hope this analysis has helped clarify how to apply the Data Analyst Cheat Sheet in practice. If anything is unclear, if you spot any mistakes, or if you have questions about the article or any piece of code, let’s discuss it and improve together! I’d be happy to connect on LinkedIn, and you can find more examples and insights on my Tableau Public profile or my blog. Looking forward to your thoughts! Tags: data analysis SQL Project