This document records the 20 analytical queries executed on the sales_db database.
Goal: Verify data loading and column structure.
SELECT * FROM retail_sales LIMIT 10;Output:
Goal: Check the volume of the dataset.
SELECT COUNT(*) AS total_orders FROM retail_sales;Output:
Goal: Calculate high-level financial metrics.
SELECT
ROUND(SUM(sales), 2) AS total_revenue,
ROUND(SUM(profit), 2) AS total_profit
FROM retail_sales;Output:
Goal: Understand the product hierarchy.
SELECT DISTINCT category FROM retail_sales;Output:
Goal: Identify key revenue drivers.
SELECT product_name, ROUND(SUM(sales), 2) AS total_sales
FROM retail_sales
GROUP BY product_name
ORDER BY total_sales DESC
LIMIT 5;Output:
Goal: Compare sales volume across regions.
SELECT region, COUNT(*) AS num_orders, ROUND(SUM(sales), 2) AS total_sales
FROM retail_sales
GROUP BY region
ORDER BY total_sales DESC;Output:
Goal: Determine the average spend per transaction.
SELECT ROUND(AVG(sales), 2) AS avg_order_value FROM retail_sales;Output:
Goal: Identify transactions with negative profit.
SELECT order_id, product_name, profit
FROM retail_sales
WHERE profit < 0
ORDER BY profit ASC
LIMIT 10;Output:
Goal: View sales performance by year.
SELECT YEAR(order_date) AS sales_year, ROUND(SUM(sales), 2) AS total_sales
FROM retail_sales
GROUP BY sales_year
ORDER BY sales_year;Output:
Goal: Detailed timeline of sales performance.
SELECT
DATE_FORMAT(order_date, '%Y-%m') AS month_year,
ROUND(SUM(sales), 2) AS monthly_sales
FROM retail_sales
GROUP BY month_year
ORDER BY month_year;Output:
Goal: Find which categories yield the highest margin.
SELECT category, ROUND(SUM(profit), 2) AS total_profit
FROM retail_sales
GROUP BY category
ORDER BY total_profit DESC
LIMIT 3;Output:
Goal: See which shipping methods are most popular.
SELECT ship_mode, ROUND(SUM(sales), 2) AS total_sales
FROM retail_sales
GROUP BY ship_mode;Output:
Goal: Identify the highest-value customer.
SELECT customer_id, customer_name, ROUND(SUM(sales), 2) AS total_spent
FROM retail_sales
GROUP BY customer_id, customer_name
ORDER BY total_spent DESC
LIMIT 1;Output:
Goal: Check which days of the week are busiest.
SELECT DAYNAME(order_date) AS day_of_week, COUNT(*) AS num_orders
FROM retail_sales
GROUP BY day_of_week
ORDER BY num_orders DESC;Output:
Goal: Spot inefficiencies where sales are high but profit is low.
SELECT sub_category, ROUND(SUM(sales), 2) AS total_sales, ROUND(SUM(profit), 2) AS total_profit
FROM retail_sales
GROUP BY sub_category
ORDER BY total_sales DESC
LIMIT 10;Output:
Goal: Filter for high-value sales in the East.
SELECT product_name, sales, region
FROM retail_sales
WHERE region = 'East' AND sales > 500;Output:
Goal: Calculate percentage profit margin per category.
SELECT
category,
ROUND((SUM(profit) / SUM(sales)) * 100, 2) AS profit_margin_percentage
FROM retail_sales
GROUP BY category
ORDER BY profit_margin_percentage DESC;Output:
Goal: Calculate cumulative sales over time.
SELECT
order_date,
sales,
SUM(sales) OVER (ORDER BY order_date) AS running_total
FROM retail_sales
LIMIT 20;Output:
Goal: Rank sub-categories by sales within their specific region.
SELECT
region,
sub_category,
ROUND(SUM(sales), 2) AS region_sales,
RANK() OVER (PARTITION BY region ORDER BY SUM(sales) DESC) AS rank_in_region
FROM retail_sales
GROUP BY region, sub_category;Output:
Goal: Compare current year sales to previous year sales.
WITH yearly_sales AS (
SELECT YEAR(order_date) AS year, SUM(sales) AS total_sales
FROM retail_sales
GROUP BY year
)
SELECT
year,
total_sales,
LAG(total_sales) OVER (ORDER BY year) AS prev_year_sales,
ROUND((total_sales - LAG(total_sales) OVER (ORDER BY year)) /
LAG(total_sales) OVER (ORDER BY year) * 100, 2) AS growth_percentage
FROM yearly_sales;Output:



















