SQL Mini Project: Sales Report
TAD Market's managers want a 2025 sales report. In this project you will build it step by step, using almost everything from the course: joins, grouping, CASE, CTEs, views and window functions. Each step is a separate playground; because every run starts from a fresh database, steps that need earlier building blocks repeat them.
The questions to answer:
- What is the value of each order, and what are the key figures (orders, revenue, average order)?
- How did revenue develop month by month?
- Which customers and countries bring the most revenue?
- How do the sales reps perform?
- Which products need attention (top sellers, never sold, low stock)?
Ground rules: revenue = quantity × products.price; cancelled orders do not count as revenue.
Step 1: order values and key figures
Start with one reusable building block — a view with the value of every order — and compute the headline numbers from it:
CREATE VIEW order_values AS
SELECT o.order_id, o.customer_id, o.employee_id, o.order_date, o.status,
SUM(oi.quantity) AS units,
ROUND(SUM(oi.quantity * p.price), 2) AS order_value
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY o.order_id;
SELECT COUNT(*) AS orders,
SUM(status = 'cancelled') AS cancelled,
ROUND(SUM(CASE WHEN status <> 'cancelled' THEN order_value END), 2) AS revenue,
ROUND(AVG(CASE WHEN status <> 'cancelled' THEN order_value END), 2) AS avg_order,
MAX(order_value) AS biggest_order
FROM order_values;orders | cancelled | revenue | avg_order | biggest_order
-------+-----------+---------+-----------+--------------
40 | 7 | 4232.71 | 128.26 | 489.5
(1 row)
CASE WHEN … THEN order_value END yields NULL for cancelled orders, and SUM/AVG skip NULLs — so one pass computes both all-order and valid-order figures.
Step 2: revenue per month
Group by month and add a running total and the change compared with the previous month:
WITH order_values AS (
SELECT o.order_id, o.order_date, o.status,
SUM(oi.quantity * p.price) AS order_value
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY o.order_id
),
monthly AS (
SELECT substr(order_date, 1, 7) AS month,
COUNT(*) AS orders,
ROUND(SUM(order_value), 2) AS revenue
FROM order_values
WHERE status <> 'cancelled'
GROUP BY month
)
SELECT month, orders, revenue,
ROUND(SUM(revenue) OVER (ORDER BY month), 2) AS year_to_date,
ROUND(revenue - LAG(revenue) OVER (ORDER BY month), 2) AS vs_prev_month
FROM monthly
ORDER BY month;month | orders | revenue | year_to_date | vs_prev_month
--------+--------+---------+--------------+--------------
2025-01 | 3 | 216.74 | 216.74 | NULL
2025-02 | 3 | 283.71 | 500.45 | 66.97
2025-03 | 3 | 340.77 | 841.22 | 57.06
2025-04 | 3 | 927.2 | 1768.42 | 586.43
2025-05 | 3 | 398.66 | 2167.08 | -528.54
2025-06 | 4 | 285.2 | 2452.28 | -113.46
2025-07 | 3 | 411.23 | 2863.51 | 126.03
2025-08 | 4 | 867.74 | 3731.25 | 456.51
2025-09 | 3 | 300.76 | 4032.01 | -566.98
2025-10 | 4 | 200.7 | 4232.71 | -100.06
(10 rows)
April and August stand out — a good question for the sales team.
Step 3: best customers and countries
WITH revenue_by_customer AS (
SELECT o.customer_id, COUNT(DISTINCT o.order_id) AS orders,
SUM(oi.quantity * p.price) AS revenue
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id
WHERE o.status <> 'cancelled'
GROUP BY o.customer_id
)
SELECT RANK() OVER (ORDER BY r.revenue DESC) AS rank,
c.name, c.country, r.orders,
ROUND(r.revenue, 2) AS revenue,
ROUND(100.0 * r.revenue / SUM(r.revenue) OVER (), 1) AS pct_of_total
FROM revenue_by_customer AS r
JOIN customers AS c ON c.customer_id = r.customer_id
ORDER BY rank
LIMIT 5;rank | name | country | orders | revenue | pct_of_total
-----+--------------+---------+--------+---------+-------------
1 | Green Fork | Canada | 2 | 660.2 | 15.6
2 | Bright Bytes | France | 4 | 655.13 | 15.5
3 | Amara Costa | Nigeria | 5 | 593.35 | 14
4 | Felix Reyes | Mexico | 4 | 450.45 | 10.6
5 | Kai Moreau | France | 3 | 362.33 | 8.6
(5 rows)
SUM(r.revenue) OVER () — a window over all rows — gives the grand total on every row, so the percentage needs no separate query. The top five customers bring in well over half of the revenue.
Step 4: sales rep performance
For every sales rep: orders handled, cancellation rate, revenue and average days to ship. Reps without orders should still appear, so the employees table drives the query:
WITH per_order AS (
SELECT o.order_id, o.employee_id, o.status,
julianday(o.shipped_date) - julianday(o.order_date) AS ship_days,
SUM(oi.quantity * p.price) AS order_value
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id
GROUP BY o.order_id
)
SELECT e.first_name || ' ' || e.last_name AS rep,
COUNT(po.order_id) AS orders,
ROUND(100.0 * SUM(po.status = 'cancelled') / COUNT(po.order_id), 1) AS cancel_pct,
ROUND(SUM(CASE WHEN po.status <> 'cancelled' THEN po.order_value END), 2) AS revenue,
ROUND(AVG(po.ship_days), 1) AS avg_ship_days
FROM employees AS e
LEFT JOIN per_order AS po ON po.employee_id = e.employee_id
WHERE e.title = 'Sales Rep'
GROUP BY e.employee_id
ORDER BY revenue DESC;rep | orders | cancel_pct | revenue | avg_ship_days
-------------+--------+------------+---------+--------------
Aisha Bello | 16 | 12.5 | 1819.93 | 2.4
Rosa Delgado | 11 | 18.2 | 1122.39 | 2.1
Owen Price | 7 | 14.3 | 861.13 | 2.3
Tom Becker | 6 | 33.3 | 429.26 | 2.5
(4 rows)
Step 5: products that need attention
One query, three lists, combined with UNION ALL and labelled:
WITH sold AS (
SELECT oi.product_id, SUM(oi.quantity) AS units
FROM order_items AS oi
JOIN orders AS o ON o.order_id = oi.order_id
WHERE o.status <> 'cancelled'
GROUP BY oi.product_id
),
top3 AS (
SELECT 'top seller' AS flag, p.name, s.units AS detail
FROM sold AS s JOIN products AS p ON p.product_id = s.product_id
ORDER BY s.units DESC, p.name
LIMIT 3
)
SELECT flag, name, detail FROM top3
UNION ALL
SELECT 'never sold', p.name, 0
FROM products AS p
WHERE NOT EXISTS (SELECT 1 FROM sold AS s WHERE s.product_id = p.product_id)
UNION ALL
SELECT 'low stock', name, stock
FROM products
WHERE stock < 10
ORDER BY flag, detail DESC, name;flag | name | detail
-----------+-----------------------------+-------
low stock | Coffee Grinder | 9
low stock | Noise-Cancelling Headphones | 8
low stock | Chocolate Cake | 6
low stock | Avocados | 0
low stock | Sparkling Water | 0
never sold | Avocados | 0
never sold | Coffee Grinder | 0
never sold | Python Recipes | 0
top seller | Sparkling Water | 21
top seller | Power Bank | 17
top seller | Bananas | 14
(11 rows)
The detail column means units sold for top sellers and units in stock for low-stock items. Python Recipes counts as "never sold" because both of its orders were cancelled — and Avocados and Sparkling Water are out of stock, so they need restocking first.
Ideas to extend the project
- Turn step 1's view into a summary table with
CREATE TABLE … AS SELECTand add a revenue column per category. - Add a
quartercolumn (see the Dates lesson) and compare Q1–Q4. - Find customers whose last order is more than 90 days before
'2025-10-31'— candidates for a "we miss you" e-mail. - Rank products within each category with
ROW_NUMBER() OVER (PARTITION BY category_id …).
Test yourself
Why does SUM(CASE WHEN status <> 'cancelled' THEN order_value END) ignore cancelled orders?
- CASE deletes those rows
- CASE returns NULL for them, and SUM skips NULLs
- SUM only counts positive values
- It doesn't ignore them
What does SUM(revenue) OVER () return on each row?
- The row's own revenue
- The running total
- The grand total over all rows
- NULL
Why start step 4 from employees with a LEFT JOIN?
- It is faster
- So reps without any orders still appear
- Employees has more columns
- LEFT JOIN removes duplicates