The Goal
You are the data person of a small online shop. The manager asks for reports. Try each task yourself before reading the solution. The database has customers, products, orders and order_items. An order's value is the sum of quantity * price of its items, and cancelled orders do not count as sales.
| Table | Main columns |
customers | id, name, city, email, joined |
products | id, name, category, price, stock |
orders | id, customer_id, order_date, status |
order_items | order_id, product_id, quantity |
Step 0: A View for Valid Sales
Nearly every report needs the same join. Save it once as a view:
sql
CREATE VIEW sales AS
SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total
FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id
WHERE o.status <> 'cancelled';
SELECT COUNT(*) AS valid_lines, ROUND(SUM(line_total), 2) AS total_revenue FROM sales;
Result:
| valid_lines | total_revenue |
| 15 | 4381.00 |
Report 1: Revenue per Month
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, COUNT(DISTINCT order_id) AS orders, ROUND(SUM(line_total), 2) AS revenue FROM sales GROUP BY DATE_FORMAT(order_date, '%Y-%m') ORDER BY month;
Result:
| month | orders | revenue |
| 2025-01 | 1 | 1057.00 |
| 2025-02 | 2 | 654.00 |
| 2025-03 | 1 | 507.00 |
| 2025-04 | 2 | 1226.00 |
| 2025-05 | 2 | 877.00 |
| 2025-06 | 1 | 60.00 |
Report 2: Top 3 Customers by Spend
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
SELECT c.name, ROUND(SUM(s.line_total), 2) AS spent FROM sales s JOIN customers c ON c.id = s.customer_id GROUP BY c.id, c.name ORDER BY spent DESC LIMIT 3;
Result:
| name | spent |
| Asha Rao | 1349.00 |
| Eli Brown | 989.00 |
| Chen Wei | 706.00 |
Report 3: Category Share of Revenue
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
SELECT category, ROUND(SUM(line_total), 2) AS revenue, ROUND(100 * SUM(line_total) / SUM(SUM(line_total)) OVER (), 1) AS pct_of_total FROM sales GROUP BY category ORDER BY revenue DESC;
Result:
| category | revenue | pct_of_total |
| Electronics | 3470.00 | 79.2 |
| Furniture | 726.00 | 16.6 |
| Stationery | 185.00 | 4.2 |
SUM(SUM(...)) OVER () is a window function over the grouped result: it gives the grand total on every row, so you can divide.
Report 4: Repeat Customers
sql
SELECT c.name, COUNT(*) AS valid_orders FROM customers c JOIN orders o ON o.customer_id = c.id WHERE o.status <> 'cancelled' GROUP BY c.id, c.name HAVING COUNT(*) >= 2 ORDER BY valid_orders DESC, c.name;
Result:
| name | valid_orders |
| Asha Rao | 3 |
| Ben Carter | 2 |
| Chen Wei | 2 |
Report 5: Products Never Sold
sql
SELECT p.name, p.stock FROM products p WHERE NOT EXISTS (SELECT 1 FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE oi.product_id = p.id AND o.status <> 'cancelled') ORDER BY p.id;
Result:
Every product has been sold at least once in this small sample, so the result is empty. That is a valid answer. Check the logic by adding a new product:
sql
INSERT INTO products VALUES (9, 'Webcam', 'Electronics', 59.00, 40);
SELECT p.name, p.stock FROM products p WHERE NOT EXISTS (SELECT 1 FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE oi.product_id = p.id AND o.status <> 'cancelled') ORDER BY p.id;
Result:
Report 6: Best Seller per Category
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
SELECT category, product, units FROM (
SELECT category, product, SUM(quantity) AS units, ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(quantity) DESC, product) AS rn FROM sales GROUP BY category, product
) t WHERE rn = 1 ORDER BY category;
Result:
| category | product | units |
| Electronics | Headphones | 6 |
| Furniture | Chair | 3 |
| Stationery | Notebook | 30 |
Report 7: Running Revenue
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
WITH monthly AS (SELECT DATE_FORMAT(order_date, '%Y-%m') AS month, SUM(line_total) AS revenue FROM sales GROUP BY DATE_FORMAT(order_date, '%Y-%m'))
SELECT month, ROUND(revenue, 2) AS revenue, ROUND(SUM(revenue) OVER (ORDER BY month), 2) AS cumulative FROM monthly ORDER BY month;
Result:
| month | revenue | cumulative |
| 2025-01 | 1057.00 | 1057.00 |
| 2025-02 | 654.00 | 1711.00 |
| 2025-03 | 507.00 | 2218.00 |
| 2025-04 | 1226.00 | 3444.00 |
| 2025-05 | 877.00 | 4321.00 |
| 2025-06 | 60.00 | 4381.00 |
Report 8: Customers by City with Spend
sql
CREATE VIEW sales AS SELECT o.id AS order_id, o.customer_id, o.order_date, p.id AS product_id, p.name AS product, p.category, oi.quantity, p.price, oi.quantity * p.price AS line_total FROM orders o JOIN order_items oi ON oi.order_id = o.id JOIN products p ON p.id = oi.product_id WHERE o.status <> 'cancelled';
SELECT COALESCE(c.city, 'Unknown') AS city, COUNT(DISTINCT c.id) AS customers, ROUND(COALESCE(SUM(s.line_total), 0.00), 2) AS revenue FROM customers c LEFT JOIN sales s ON s.customer_id = c.id GROUP BY COALESCE(c.city, 'Unknown') ORDER BY revenue DESC, city;
Result:
| city | customers | revenue |
| Mumbai | 2 | 2027.00 |
| London | 2 | 1648.00 |
| Singapore | 1 | 706.00 |
| Tel Aviv | 1 | 0.00 |
Report 9: Days Between Orders per Customer
sql
SELECT c.name, o.order_date, DATEDIFF(o.order_date, LAG(o.order_date) OVER (PARTITION BY o.customer_id ORDER BY o.order_date)) AS days_since_previous
FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status <> 'cancelled' AND c.id IN (1, 2) ORDER BY c.id, o.order_date;
Result:
| name | order_date | days_since_previous |
| Asha Rao | 2025-01-10 | NULL |
| Asha Rao | 2025-02-14 | 35 |
| Asha Rao | 2025-04-20 | 65 |
| Ben Carter | 2025-02-20 | NULL |
| Ben Carter | 2025-06-01 | 101 |
Report 10: Low-Stock Alert with Pending Demand
sql
SELECT p.name, p.stock, COALESCE(SUM(oi.quantity), 0) AS pending_units, p.stock - COALESCE(SUM(oi.quantity), 0) AS left_after_pending
FROM products p LEFT JOIN order_items oi ON oi.product_id = p.id LEFT JOIN orders o ON o.id = oi.order_id AND o.status = 'pending'
WHERE o.id IS NOT NULL OR oi.product_id IS NULL GROUP BY p.id, p.name, p.stock ORDER BY left_after_pending, p.name LIMIT 5;
Result:
| name | stock | pending_units | left_after_pending |
| Desk | 10 | 1 | 9 |
| Chair | 25 | 2 | 23 |
| Headphones | 100 | 3 | 97 |
What You Practiced
- Views to avoid repeating a join
GROUP BY and HAVING
- Window functions for share, ranking, running totals and
LAG
NOT EXISTS for missing data
- Safe handling of NULLs with
COALESCE
- Checking a result that comes back empty
Extra Challenges
- Add a column
customer_segment (VIP above 1000, Regular above 200, else New) with CASE.
- Find the month with the best revenue growth over the previous month.
- Show the top product per city.
- Turn the view into a stored procedure with a date range parameter.
Conclusion
You built ten real reports from one reusable view, using joins, grouping, window functions and anti joins. These patterns cover most analytics requests.
Quick Summary: view, GROUP BY, HAVING, window functions, NOT EXISTS, COALESCE, LAG.