Case 1: a KPI overview in SQL
Every analysis starts with the same questions. How much revenue, how many orders, how many customers, what's the average order value? These numbers are quick to calculate and just as quick to get wrong. In this case you build them so they hold up.
In short: revenue is SUM(amount), average order value is AVG(amount) on the orders table, customers are COUNT(DISTINCT customer_id). Filter out cancellations with WHERE before anything is calculated. For months, group by strftime('%Y-%m', order_date); for channels, start from the customers table with a LEFT JOIN.
The situation
The kitchenware store has just finished its first full year. For next year's plan, management wants three things: the headline numbers for the year, how they moved month by month, and a comparison of the channels customers signed up through. Until now someone pieced this together from a spreadsheet export every month. From now on it should be a query anyone on the team can rerun.
The data
You need two tables here. orders has one row per order with customer_id, order_date, status and amount, the net order value. customers includes the channel each customer signed up through. Before adding anything up, look at the status column:
SELECT status, COUNT(*) AS orders, ROUND(SUM(amount), 2) AS amount
FROM orders
GROUP BY status
ORDER BY orders DESC;
| status | orders | amount |
|---|---|---|
| delivered | 111 | 15923.8 |
| cancelled | 9 | 935.6 |
| shipped | 2 | 278.3 |
Nine orders were cancelled and don't belong in revenue. Shipped orders do: they just haven't arrived yet. So throughout the course the filter is WHERE status <> 'cancelled'.
Task 1: the headline numbers
Task Management wants the headline numbers for the year in a single row. Include every order that isn't cancelled and show: orders (count), revenue (sum of amount, rounded to 2 decimals), avg_order_value (average amount per order, rounded to 2 decimals) and customers (number of different customers who ordered).
orders 122 rows, 5 columns
| order_id | customer_id | order_date | status | amount |
|---|---|---|---|---|
| 5001 | 2 | 2025-11-01 | cancelled | 93.9 |
| 5002 | 4 | 2025-11-02 | delivered | 238.8 |
| 5003 | 6 | 2025-11-08 | delivered | 14.9 |
| 5004 | 3 | 2025-11-10 | delivered | 29.9 |
| 5005 | 7 | 2025-11-11 | delivered | 44.9 |
| 5006 | 5 | 2025-11-13 | delivered | 104.7 |
| 5007 | 11 | 2025-11-16 | delivered | 17.9 |
| 5008 | 13 | 2025-11-17 | delivered | 119 |
| 5009 | 9 | 2025-11-18 | delivered | 206.8 |
| 5010 | 10 | 2025-11-20 | delivered | 758 |
| 5011 | 12 | 2025-11-23 | delivered | 44.9 |
| 5012 | 1 | 2025-11-25 | delivered | 77.8 |
| 5013 | 4 | 2025-11-27 | delivered | 149 |
| 5014 | 8 | 2025-11-28 | delivered | 39.9 |
| 5015 | 10 | 2025-12-15 | delivered | 777.9 |
| 5016 | 14 | 2025-12-16 | delivered | 55.8 |
| 5017 | 2 | 2025-12-22 | delivered | 34.9 |
| 5018 | 16 | 2025-12-22 | delivered | 79 |
| 5019 | 15 | 2025-12-29 | delivered | 22.9 |
| 5020 | 20 | 2025-12-29 | delivered | 59.8 |
| 5021 | 21 | 2025-12-30 | delivered | 379 |
| 5022 | 19 | 2025-12-31 | delivered | 114.7 |
| 5023 | 17 | 2026-01-03 | delivered | 49.8 |
| 5024 | 18 | 2026-01-08 | delivered | 79 |
| 5025 | 6 | 2026-01-14 | delivered | 44.9 |
| 5026 | 11 | 2026-01-16 | delivered | 12.9 |
| 5027 | 9 | 2026-01-17 | delivered | 49.9 |
| 5028 | 2 | 2026-01-21 | delivered | 59.9 |
| 5029 | 22 | 2026-01-24 | delivered | 49.9 |
| 5030 | 24 | 2026-01-25 | delivered | 498 |
| 5031 | 25 | 2026-02-04 | delivered | 413.9 |
| 5032 | 5 | 2026-02-05 | delivered | 34.9 |
| 5033 | 28 | 2026-02-06 | cancelled | 34.9 |
| 5034 | 26 | 2026-02-11 | delivered | 17.9 |
| 5035 | 11 | 2026-02-20 | delivered | 39.9 |
| 5036 | 21 | 2026-02-23 | delivered | 170.9 |
| 5037 | 29 | 2026-02-25 | delivered | 49.9 |
| 5038 | 27 | 2026-02-26 | delivered | 24.9 |
| 5039 | 12 | 2026-03-03 | delivered | 308 |
| 5040 | 18 | 2026-03-03 | delivered | 26.9 |
| 5041 | 15 | 2026-03-06 | delivered | 99.6 |
| 5042 | 1 | 2026-03-07 | delivered | 403.8 |
| 5043 | 31 | 2026-03-11 | delivered | 417.7 |
| 5044 | 30 | 2026-03-12 | delivered | 149 |
| 5045 | 33 | 2026-03-13 | delivered | 158 |
| 5046 | 34 | 2026-03-19 | delivered | 189 |
| 5047 | 32 | 2026-03-27 | delivered | 123.8 |
| 5048 | 35 | 2026-03-30 | delivered | 93.9 |
| 5049 | 19 | 2026-04-01 | delivered | 69.8 |
| 5050 | 2 | 2026-04-04 | cancelled | 368.9 |
| 5051 | 34 | 2026-04-06 | delivered | 47.8 |
| 5052 | 20 | 2026-04-16 | delivered | 119 |
| 5053 | 6 | 2026-04-22 | delivered | 258.7 |
| 5054 | 2 | 2026-04-23 | cancelled | 126.7 |
| 5055 | 11 | 2026-04-24 | delivered | 29.9 |
| 5056 | 36 | 2026-04-26 | delivered | 79 |
| 5057 | 37 | 2026-04-26 | cancelled | 12.9 |
| 5058 | 39 | 2026-04-27 | delivered | 117.7 |
| 5059 | 38 | 2026-04-29 | delivered | 19.9 |
| 5060 | 22 | 2026-04-30 | delivered | 59.9 |
| 5061 | 99 | 2026-05-01 | delivered | 63.7 |
| 5062 | 40 | 2026-05-04 | delivered | 158 |
| 5063 | 41 | 2026-05-05 | delivered | 378 |
| 5064 | 29 | 2026-05-14 | delivered | 228.7 |
| 5065 | 43 | 2026-05-17 | delivered | 74.7 |
| 5066 | 45 | 2026-05-25 | cancelled | 39.9 |
| 5067 | 44 | 2026-05-27 | delivered | 69.8 |
| 5068 | 46 | 2026-05-31 | delivered | 80.7 |
| 5069 | 27 | 2026-06-04 | delivered | 97.7 |
| 5070 | 31 | 2026-06-09 | delivered | 68.7 |
| 5071 | 18 | 2026-06-10 | delivered | 189 |
| 5072 | 19 | 2026-06-11 | delivered | 57.7 |
| 5073 | 35 | 2026-06-13 | delivered | 149 |
| 5074 | 47 | 2026-06-19 | delivered | 119 |
| 5075 | 49 | 2026-06-20 | delivered | 39.9 |
| 5076 | 48 | 2026-06-23 | delivered | 49.9 |
| 5077 | 42 | 2026-06-25 | delivered | 19.9 |
| 5078 | 51 | 2026-06-27 | delivered | 34.9 |
| 5079 | 39 | 2026-07-01 | delivered | 59.7 |
| 5080 | 50 | 2026-07-01 | delivered | 25.8 |
| 5081 | 45 | 2026-07-02 | delivered | 188.8 |
| 5082 | 19 | 2026-07-04 | delivered | 134.4 |
| 5083 | 53 | 2026-07-12 | delivered | 83.7 |
| 5084 | 52 | 2026-07-14 | delivered | 77.8 |
| 5085 | 38 | 2026-07-15 | delivered | 129.7 |
| 5086 | 54 | 2026-07-15 | delivered | 418.8 |
| 5087 | 46 | 2026-07-17 | delivered | 59.8 |
| 5088 | 29 | 2026-07-19 | delivered | 188.9 |
| 5089 | 36 | 2026-07-19 | delivered | 124.7 |
| 5090 | 45 | 2026-07-27 | delivered | 14.9 |
| 5091 | 41 | 2026-07-29 | delivered | 19.9 |
| 5092 | 58 | 2026-08-01 | delivered | 99.8 |
| 5093 | 56 | 2026-08-05 | delivered | 74.7 |
| 5094 | 53 | 2026-08-06 | delivered | 98.9 |
| 5095 | 59 | 2026-08-07 | delivered | 22.9 |
| 5096 | 18 | 2026-08-12 | delivered | 1157 |
| 5097 | 61 | 2026-08-16 | delivered | 44.7 |
| 5098 | 29 | 2026-08-20 | delivered | 105.7 |
| 5099 | 60 | 2026-08-24 | delivered | 307 |
| 5100 | 39 | 2026-08-27 | delivered | 103.9 |
| 5101 | 44 | 2026-08-28 | delivered | 243.8 |
| 5102 | 31 | 2026-08-31 | delivered | 128.9 |
| 5103 | 54 | 2026-08-31 | delivered | 298 |
| 5104 | 42 | 2026-09-01 | delivered | 388.9 |
| 5105 | 47 | 2026-09-01 | delivered | 124.7 |
| 5106 | 63 | 2026-09-06 | cancelled | 14.9 |
| 5107 | 64 | 2026-09-06 | delivered | 62.8 |
| 5108 | 65 | 2026-09-06 | cancelled | 208.6 |
| 5109 | 62 | 2026-09-11 | delivered | 34.9 |
| 5110 | 27 | 2026-09-18 | delivered | 29.9 |
| 5111 | 42 | 2026-09-20 | delivered | 104.7 |
| 5112 | 41 | 2026-09-21 | delivered | 24.9 |
| 5113 | 46 | 2026-09-27 | delivered | 94.6 |
| 5114 | 66 | 2026-09-28 | delivered | 29.9 |
| 5115 | 67 | 2026-10-01 | delivered | 34.9 |
| 5116 | 51 | 2026-10-07 | delivered | 69.8 |
| 5117 | 53 | 2026-10-07 | cancelled | 34.9 |
| 5118 | 70 | 2026-10-09 | delivered | 211.9 |
| 5119 | 64 | 2026-10-16 | delivered | 698 |
| 5120 | 68 | 2026-10-21 | delivered | 183.9 |
| 5121 | 69 | 2026-10-27 | shipped | 159.4 |
| 5122 | 27 | 2026-10-30 | shipped | 118.9 |
Four KPIs, one row, no grouping. Without GROUP BY, the aggregate functions collapse every row left after WHERE into one. Average order value is an average over orders. If you averaged over order_items instead, you'd get the value per line, which is much smaller for orders with several products. For customers, each customer_id counts once, because plenty of customers ordered more than once.
Task 2: KPIs by month
For the monthly series you add a grouping. The month is part of the date: strftime('%Y-%m', order_date) turns 2026-03-14 into the text 2026-03. With the year first, ORDER BY sorts months correctly across the turn of the year too. There's more on dates in the lesson on SQL date functions and more on grouping under GROUP BY.
Task For the monthly report the team needs the headline numbers by month. For all orders that aren't cancelled, show: month (as text YYYY-MM), orders, revenue (sum of amount, 2 decimals) and avg_order_value (average of amount, 2 decimals), sorted by month.
orders 122 rows, 5 columns
| order_id | customer_id | order_date | status | amount |
|---|---|---|---|---|
| 5001 | 2 | 2025-11-01 | cancelled | 93.9 |
| 5002 | 4 | 2025-11-02 | delivered | 238.8 |
| 5003 | 6 | 2025-11-08 | delivered | 14.9 |
| 5004 | 3 | 2025-11-10 | delivered | 29.9 |
| 5005 | 7 | 2025-11-11 | delivered | 44.9 |
| 5006 | 5 | 2025-11-13 | delivered | 104.7 |
| 5007 | 11 | 2025-11-16 | delivered | 17.9 |
| 5008 | 13 | 2025-11-17 | delivered | 119 |
| 5009 | 9 | 2025-11-18 | delivered | 206.8 |
| 5010 | 10 | 2025-11-20 | delivered | 758 |
| 5011 | 12 | 2025-11-23 | delivered | 44.9 |
| 5012 | 1 | 2025-11-25 | delivered | 77.8 |
| 5013 | 4 | 2025-11-27 | delivered | 149 |
| 5014 | 8 | 2025-11-28 | delivered | 39.9 |
| 5015 | 10 | 2025-12-15 | delivered | 777.9 |
| 5016 | 14 | 2025-12-16 | delivered | 55.8 |
| 5017 | 2 | 2025-12-22 | delivered | 34.9 |
| 5018 | 16 | 2025-12-22 | delivered | 79 |
| 5019 | 15 | 2025-12-29 | delivered | 22.9 |
| 5020 | 20 | 2025-12-29 | delivered | 59.8 |
| 5021 | 21 | 2025-12-30 | delivered | 379 |
| 5022 | 19 | 2025-12-31 | delivered | 114.7 |
| 5023 | 17 | 2026-01-03 | delivered | 49.8 |
| 5024 | 18 | 2026-01-08 | delivered | 79 |
| 5025 | 6 | 2026-01-14 | delivered | 44.9 |
| 5026 | 11 | 2026-01-16 | delivered | 12.9 |
| 5027 | 9 | 2026-01-17 | delivered | 49.9 |
| 5028 | 2 | 2026-01-21 | delivered | 59.9 |
| 5029 | 22 | 2026-01-24 | delivered | 49.9 |
| 5030 | 24 | 2026-01-25 | delivered | 498 |
| 5031 | 25 | 2026-02-04 | delivered | 413.9 |
| 5032 | 5 | 2026-02-05 | delivered | 34.9 |
| 5033 | 28 | 2026-02-06 | cancelled | 34.9 |
| 5034 | 26 | 2026-02-11 | delivered | 17.9 |
| 5035 | 11 | 2026-02-20 | delivered | 39.9 |
| 5036 | 21 | 2026-02-23 | delivered | 170.9 |
| 5037 | 29 | 2026-02-25 | delivered | 49.9 |
| 5038 | 27 | 2026-02-26 | delivered | 24.9 |
| 5039 | 12 | 2026-03-03 | delivered | 308 |
| 5040 | 18 | 2026-03-03 | delivered | 26.9 |
| 5041 | 15 | 2026-03-06 | delivered | 99.6 |
| 5042 | 1 | 2026-03-07 | delivered | 403.8 |
| 5043 | 31 | 2026-03-11 | delivered | 417.7 |
| 5044 | 30 | 2026-03-12 | delivered | 149 |
| 5045 | 33 | 2026-03-13 | delivered | 158 |
| 5046 | 34 | 2026-03-19 | delivered | 189 |
| 5047 | 32 | 2026-03-27 | delivered | 123.8 |
| 5048 | 35 | 2026-03-30 | delivered | 93.9 |
| 5049 | 19 | 2026-04-01 | delivered | 69.8 |
| 5050 | 2 | 2026-04-04 | cancelled | 368.9 |
| 5051 | 34 | 2026-04-06 | delivered | 47.8 |
| 5052 | 20 | 2026-04-16 | delivered | 119 |
| 5053 | 6 | 2026-04-22 | delivered | 258.7 |
| 5054 | 2 | 2026-04-23 | cancelled | 126.7 |
| 5055 | 11 | 2026-04-24 | delivered | 29.9 |
| 5056 | 36 | 2026-04-26 | delivered | 79 |
| 5057 | 37 | 2026-04-26 | cancelled | 12.9 |
| 5058 | 39 | 2026-04-27 | delivered | 117.7 |
| 5059 | 38 | 2026-04-29 | delivered | 19.9 |
| 5060 | 22 | 2026-04-30 | delivered | 59.9 |
| 5061 | 99 | 2026-05-01 | delivered | 63.7 |
| 5062 | 40 | 2026-05-04 | delivered | 158 |
| 5063 | 41 | 2026-05-05 | delivered | 378 |
| 5064 | 29 | 2026-05-14 | delivered | 228.7 |
| 5065 | 43 | 2026-05-17 | delivered | 74.7 |
| 5066 | 45 | 2026-05-25 | cancelled | 39.9 |
| 5067 | 44 | 2026-05-27 | delivered | 69.8 |
| 5068 | 46 | 2026-05-31 | delivered | 80.7 |
| 5069 | 27 | 2026-06-04 | delivered | 97.7 |
| 5070 | 31 | 2026-06-09 | delivered | 68.7 |
| 5071 | 18 | 2026-06-10 | delivered | 189 |
| 5072 | 19 | 2026-06-11 | delivered | 57.7 |
| 5073 | 35 | 2026-06-13 | delivered | 149 |
| 5074 | 47 | 2026-06-19 | delivered | 119 |
| 5075 | 49 | 2026-06-20 | delivered | 39.9 |
| 5076 | 48 | 2026-06-23 | delivered | 49.9 |
| 5077 | 42 | 2026-06-25 | delivered | 19.9 |
| 5078 | 51 | 2026-06-27 | delivered | 34.9 |
| 5079 | 39 | 2026-07-01 | delivered | 59.7 |
| 5080 | 50 | 2026-07-01 | delivered | 25.8 |
| 5081 | 45 | 2026-07-02 | delivered | 188.8 |
| 5082 | 19 | 2026-07-04 | delivered | 134.4 |
| 5083 | 53 | 2026-07-12 | delivered | 83.7 |
| 5084 | 52 | 2026-07-14 | delivered | 77.8 |
| 5085 | 38 | 2026-07-15 | delivered | 129.7 |
| 5086 | 54 | 2026-07-15 | delivered | 418.8 |
| 5087 | 46 | 2026-07-17 | delivered | 59.8 |
| 5088 | 29 | 2026-07-19 | delivered | 188.9 |
| 5089 | 36 | 2026-07-19 | delivered | 124.7 |
| 5090 | 45 | 2026-07-27 | delivered | 14.9 |
| 5091 | 41 | 2026-07-29 | delivered | 19.9 |
| 5092 | 58 | 2026-08-01 | delivered | 99.8 |
| 5093 | 56 | 2026-08-05 | delivered | 74.7 |
| 5094 | 53 | 2026-08-06 | delivered | 98.9 |
| 5095 | 59 | 2026-08-07 | delivered | 22.9 |
| 5096 | 18 | 2026-08-12 | delivered | 1157 |
| 5097 | 61 | 2026-08-16 | delivered | 44.7 |
| 5098 | 29 | 2026-08-20 | delivered | 105.7 |
| 5099 | 60 | 2026-08-24 | delivered | 307 |
| 5100 | 39 | 2026-08-27 | delivered | 103.9 |
| 5101 | 44 | 2026-08-28 | delivered | 243.8 |
| 5102 | 31 | 2026-08-31 | delivered | 128.9 |
| 5103 | 54 | 2026-08-31 | delivered | 298 |
| 5104 | 42 | 2026-09-01 | delivered | 388.9 |
| 5105 | 47 | 2026-09-01 | delivered | 124.7 |
| 5106 | 63 | 2026-09-06 | cancelled | 14.9 |
| 5107 | 64 | 2026-09-06 | delivered | 62.8 |
| 5108 | 65 | 2026-09-06 | cancelled | 208.6 |
| 5109 | 62 | 2026-09-11 | delivered | 34.9 |
| 5110 | 27 | 2026-09-18 | delivered | 29.9 |
| 5111 | 42 | 2026-09-20 | delivered | 104.7 |
| 5112 | 41 | 2026-09-21 | delivered | 24.9 |
| 5113 | 46 | 2026-09-27 | delivered | 94.6 |
| 5114 | 66 | 2026-09-28 | delivered | 29.9 |
| 5115 | 67 | 2026-10-01 | delivered | 34.9 |
| 5116 | 51 | 2026-10-07 | delivered | 69.8 |
| 5117 | 53 | 2026-10-07 | cancelled | 34.9 |
| 5118 | 70 | 2026-10-09 | delivered | 211.9 |
| 5119 | 64 | 2026-10-16 | delivered | 698 |
| 5120 | 68 | 2026-10-21 | delivered | 183.9 |
| 5121 | 69 | 2026-10-27 | shipped | 159.4 |
| 5122 | 27 | 2026-10-30 | shipped | 118.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Take a close look at the result. August stands out with over 2,600 in revenue and an average order above 220. Before you report that as a win: a single order can distort a month like this. Whether the number is right is something you'll find out in case 6, data quality.
Task 3: KPIs by signup channel
Now the question is about customers, not orders. "How many customers came through search, and how many of them bought?" needs the customers who never ordered as well. So you start from customers and attach orders with a LEFT JOIN. Customers without an order stay in the result, with NULL in the order columns.
Task Marketing wants to know which signup channel brings the best customers. For each channel in the customers table, show: customers (all registered customers from that channel), buyers (customers from that channel with at least one order that isn't cancelled) and revenue (sum of amount of their non-cancelled orders, 2 decimals). Customers without an order still count under customers.
customers 70 rows, 6 columns
| customer_id | name | city | signup_date | channel | |
|---|---|---|---|---|---|
| 1 | Samuel Ward | samuel.ward@example.com | Austin | 2025-10-07 | Newsletter |
| 2 | Owen Robinson | owen.robinson@example.com | Denver | 2025-08-02 | Referral |
| 3 | Liam Martin | liam.martin@example.com | Chicago | 2025-09-17 | Search |
| 4 | Thomas Hughes | thomas.hughes@example.com | Bristol | 2025-09-07 | Search |
| 5 | Chloe Lewis | chloe.lewis@example.com | Bristol | 2025-09-03 | Newsletter |
| 6 | Connor Hall | connor.hall@example.com | New York | 2025-07-07 | Referral |
| 7 | Lily Robinson | lily.robinson@example.com | Glasgow | 2025-07-31 | Newsletter |
| 8 | James Thomas | james.thomas@example.com | Glasgow | 2025-07-02 | Search |
| 9 | Aisha Green | aisha.green@example.com | Chicago | 2025-07-16 | Newsletter |
| 10 | Sophia Murphy | sophia.murphy@example.com | Birmingham | 2025-08-03 | Search |
| 11 | Emily Harris | emily.harris@example.com | Leeds | 2025-11-08 | Search |
| 12 | Hannah Thomas | hannah.thomas@example.com | NULL | 2025-11-16 | Search |
| 13 | Ethan Hall | ethan.hall@example.com | Austin | 2025-11-13 | Search |
| 14 | Lucas Price | lucas.price@example.com | Leeds | 2025-11-26 | Newsletter |
| 15 | Priya Harris | priya.harris@example.com | New York | 2025-12-05 | Newsletter |
| 16 | Lucas Reed | lucas.reed@example.com | Seattle | 2025-12-01 | Search |
| 17 | Fiona Campbell | fiona.campbell@example.com | Denver | 2025-12-11 | Search |
| 18 | Ryan Wright | ryan.wright@example.com | Chicago | 2025-12-14 | Social media |
| 19 | Priya Evans | priya.evans@example.com | Denver | 2025-12-26 | Search |
| 20 | Grace Campbell | grace.campbell@example.com | Denver | 2025-12-26 | Search |
| 21 | Aisha Price | aisha.price@example.com | New York | 2025-12-30 | Referral |
| 22 | Zoe Harris | zoe.harris@example.com | New York | 2026-01-11 | Newsletter |
| 23 | Liam Brown | liam.brown@example.com | Manchester | 2026-01-15 | Newsletter |
| 24 | Olivia Taylor | olivia.taylor@example.com | Denver | 2026-02-03 | Search |
| 25 | Aisha Wilson | aisha.wilson@example.com | Austin | 2026-02-13 | Search |
| 26 | Megan Reed | megan.reed@example.com | Glasgow | 2026-01-28 | Search |
| 27 | Amelia Green | NULL | Bristol | 2026-02-04 | Search |
| 28 | Kevin Wilson | kevin.wilson@example.com | Birmingham | 2026-02-06 | Search |
| 29 | Isla Morgan | isla.morgan@example.com | London | 2026-02-11 | Search |
| 30 | Liam Hall | liam.hall@example.com | Bristol | 2026-02-22 | Referral |
| 31 | Zoe Turner | zoe.turner@example.com | Birmingham | 2026-02-24 | Search |
| 32 | Daniel Evans | daniel.evans@example.com | Chicago | 2026-03-05 | Newsletter |
| 33 | Emily Wright | emily.wright@example.com | London | 2026-03-07 | Search |
| 34 | Connor Taylor | connor.taylor@example.com | Seattle | 2026-03-11 | Search |
| 35 | Olivia Reed | olivia.reed@example.com | New York | 2026-03-24 | Social media |
| 36 | Kevin Martin | kevin.martin@example.com | Birmingham | 2026-04-01 | Referral |
| 37 | Emily Davies | emily.davies@example.com | Austin | 2026-04-09 | Search |
| 38 | Ruby Johnson | ruby.johnson@example.com | NULL | 2026-04-08 | Social media |
| 39 | Henry Campbell | henry.campbell@example.com | Glasgow | 2026-04-10 | Newsletter |
| 40 | Ethan Murphy | ethan.murphy@example.com | Bristol | 2026-04-24 | Newsletter |
| 41 | Ethan Martin | ethan.martin@example.com | Manchester | 2026-05-01 | Referral |
| 42 | Liam Campbell | liam.campbell@example.com | Austin | 2026-04-30 | Search |
| 43 | Holly Harris | holly.harris@example.com | New York | 2026-05-10 | Search |
| 44 | Aaron Smith | aaron.smith@example.com | Manchester | 2026-05-13 | Newsletter |
| 45 | Aisha Baker | aisha.baker@example.com | Denver | 2026-05-19 | Social media |
| 46 | Isla Bennett | isla.bennett@example.com | Glasgow | 2026-05-18 | Social media |
| 47 | Chloe Davies | chloe.davies@example.com | Leeds | 2026-06-05 | Newsletter |
| 48 | Thomas Robinson | thomas.robinson@example.com | Boston | 2026-06-03 | Search |
| 49 | Marcus Evans | marcus.evans@example.com | Manchester | 2026-06-15 | Newsletter |
| 50 | Oliver Wright | oliver.wright@example.com | Bristol | 2026-06-10 | Search |
| 51 | Thomas Johnson | thomas.johnson@example.com | Glasgow | 2026-06-18 | Search |
| 52 | Sophia Reed | sophia.reed@example.com | London | 2026-06-24 | Search |
| 53 | Fiona Clarke | fiona.clarke@example.com | Leeds | 2026-07-02 | Referral |
| 54 | Liam Hall | LIAM.HALL@EXAMPLE.COM | Bristol | 2026-07-08 | Search |
| 55 | Lily Thomas | lily.thomas@example.com | Boston | 2026-07-14 | Search |
| 56 | Chloe Robinson | chloe.robinson@example.com | Bristol | 2026-07-26 | Search |
| 57 | Hannah Campbell | hannah.campbell@example.com | Boston | 2026-07-20 | Social media |
| 58 | Ruby Morgan | ruby.morgan@example.com | New York | 2026-07-29 | Search |
| 59 | Sophia Smith | sophia.smith@example.com | London | 2026-07-31 | Newsletter |
| 60 | Ava Martin | ava.martin@example.com | Birmingham | 2026-08-11 | Social media |
| 61 | Jack Hall | jack.hall@example.com | NULL | 2026-08-13 | Newsletter |
| 62 | Grace Cooper | grace.cooper@example.com | Chicago | 2026-08-25 | Search |
| 63 | Connor Price | connor.price@example.com | Manchester | 2026-08-23 | Search |
| 64 | Marcus Walker | marcus.walker@example.com | Bristol | 2026-09-04 | Social media |
| 65 | Holly Brown | holly.brown@example.com | Denver | 2026-09-06 | Search |
| 66 | Priya Evans | Priya.Evans@example.com | Denver | 2026-09-19 | Search |
| 67 | Carlos Garcia | carlos.garcia@example.com | Chicago | 2026-09-19 | Social media |
| 68 | Priya Thomas | priya.thomas@example.com | Bristol | 2026-09-29 | Search |
| 69 | Mia Reed | mia.reed@example.com | Chicago | 2026-10-02 | Search |
| 70 | Marcus Turner | marcus.turner@example.com | Boston | 2026-10-04 | Newsletter |
orders 122 rows, 5 columns
| order_id | customer_id | order_date | status | amount |
|---|---|---|---|---|
| 5001 | 2 | 2025-11-01 | cancelled | 93.9 |
| 5002 | 4 | 2025-11-02 | delivered | 238.8 |
| 5003 | 6 | 2025-11-08 | delivered | 14.9 |
| 5004 | 3 | 2025-11-10 | delivered | 29.9 |
| 5005 | 7 | 2025-11-11 | delivered | 44.9 |
| 5006 | 5 | 2025-11-13 | delivered | 104.7 |
| 5007 | 11 | 2025-11-16 | delivered | 17.9 |
| 5008 | 13 | 2025-11-17 | delivered | 119 |
| 5009 | 9 | 2025-11-18 | delivered | 206.8 |
| 5010 | 10 | 2025-11-20 | delivered | 758 |
| 5011 | 12 | 2025-11-23 | delivered | 44.9 |
| 5012 | 1 | 2025-11-25 | delivered | 77.8 |
| 5013 | 4 | 2025-11-27 | delivered | 149 |
| 5014 | 8 | 2025-11-28 | delivered | 39.9 |
| 5015 | 10 | 2025-12-15 | delivered | 777.9 |
| 5016 | 14 | 2025-12-16 | delivered | 55.8 |
| 5017 | 2 | 2025-12-22 | delivered | 34.9 |
| 5018 | 16 | 2025-12-22 | delivered | 79 |
| 5019 | 15 | 2025-12-29 | delivered | 22.9 |
| 5020 | 20 | 2025-12-29 | delivered | 59.8 |
| 5021 | 21 | 2025-12-30 | delivered | 379 |
| 5022 | 19 | 2025-12-31 | delivered | 114.7 |
| 5023 | 17 | 2026-01-03 | delivered | 49.8 |
| 5024 | 18 | 2026-01-08 | delivered | 79 |
| 5025 | 6 | 2026-01-14 | delivered | 44.9 |
| 5026 | 11 | 2026-01-16 | delivered | 12.9 |
| 5027 | 9 | 2026-01-17 | delivered | 49.9 |
| 5028 | 2 | 2026-01-21 | delivered | 59.9 |
| 5029 | 22 | 2026-01-24 | delivered | 49.9 |
| 5030 | 24 | 2026-01-25 | delivered | 498 |
| 5031 | 25 | 2026-02-04 | delivered | 413.9 |
| 5032 | 5 | 2026-02-05 | delivered | 34.9 |
| 5033 | 28 | 2026-02-06 | cancelled | 34.9 |
| 5034 | 26 | 2026-02-11 | delivered | 17.9 |
| 5035 | 11 | 2026-02-20 | delivered | 39.9 |
| 5036 | 21 | 2026-02-23 | delivered | 170.9 |
| 5037 | 29 | 2026-02-25 | delivered | 49.9 |
| 5038 | 27 | 2026-02-26 | delivered | 24.9 |
| 5039 | 12 | 2026-03-03 | delivered | 308 |
| 5040 | 18 | 2026-03-03 | delivered | 26.9 |
| 5041 | 15 | 2026-03-06 | delivered | 99.6 |
| 5042 | 1 | 2026-03-07 | delivered | 403.8 |
| 5043 | 31 | 2026-03-11 | delivered | 417.7 |
| 5044 | 30 | 2026-03-12 | delivered | 149 |
| 5045 | 33 | 2026-03-13 | delivered | 158 |
| 5046 | 34 | 2026-03-19 | delivered | 189 |
| 5047 | 32 | 2026-03-27 | delivered | 123.8 |
| 5048 | 35 | 2026-03-30 | delivered | 93.9 |
| 5049 | 19 | 2026-04-01 | delivered | 69.8 |
| 5050 | 2 | 2026-04-04 | cancelled | 368.9 |
| 5051 | 34 | 2026-04-06 | delivered | 47.8 |
| 5052 | 20 | 2026-04-16 | delivered | 119 |
| 5053 | 6 | 2026-04-22 | delivered | 258.7 |
| 5054 | 2 | 2026-04-23 | cancelled | 126.7 |
| 5055 | 11 | 2026-04-24 | delivered | 29.9 |
| 5056 | 36 | 2026-04-26 | delivered | 79 |
| 5057 | 37 | 2026-04-26 | cancelled | 12.9 |
| 5058 | 39 | 2026-04-27 | delivered | 117.7 |
| 5059 | 38 | 2026-04-29 | delivered | 19.9 |
| 5060 | 22 | 2026-04-30 | delivered | 59.9 |
| 5061 | 99 | 2026-05-01 | delivered | 63.7 |
| 5062 | 40 | 2026-05-04 | delivered | 158 |
| 5063 | 41 | 2026-05-05 | delivered | 378 |
| 5064 | 29 | 2026-05-14 | delivered | 228.7 |
| 5065 | 43 | 2026-05-17 | delivered | 74.7 |
| 5066 | 45 | 2026-05-25 | cancelled | 39.9 |
| 5067 | 44 | 2026-05-27 | delivered | 69.8 |
| 5068 | 46 | 2026-05-31 | delivered | 80.7 |
| 5069 | 27 | 2026-06-04 | delivered | 97.7 |
| 5070 | 31 | 2026-06-09 | delivered | 68.7 |
| 5071 | 18 | 2026-06-10 | delivered | 189 |
| 5072 | 19 | 2026-06-11 | delivered | 57.7 |
| 5073 | 35 | 2026-06-13 | delivered | 149 |
| 5074 | 47 | 2026-06-19 | delivered | 119 |
| 5075 | 49 | 2026-06-20 | delivered | 39.9 |
| 5076 | 48 | 2026-06-23 | delivered | 49.9 |
| 5077 | 42 | 2026-06-25 | delivered | 19.9 |
| 5078 | 51 | 2026-06-27 | delivered | 34.9 |
| 5079 | 39 | 2026-07-01 | delivered | 59.7 |
| 5080 | 50 | 2026-07-01 | delivered | 25.8 |
| 5081 | 45 | 2026-07-02 | delivered | 188.8 |
| 5082 | 19 | 2026-07-04 | delivered | 134.4 |
| 5083 | 53 | 2026-07-12 | delivered | 83.7 |
| 5084 | 52 | 2026-07-14 | delivered | 77.8 |
| 5085 | 38 | 2026-07-15 | delivered | 129.7 |
| 5086 | 54 | 2026-07-15 | delivered | 418.8 |
| 5087 | 46 | 2026-07-17 | delivered | 59.8 |
| 5088 | 29 | 2026-07-19 | delivered | 188.9 |
| 5089 | 36 | 2026-07-19 | delivered | 124.7 |
| 5090 | 45 | 2026-07-27 | delivered | 14.9 |
| 5091 | 41 | 2026-07-29 | delivered | 19.9 |
| 5092 | 58 | 2026-08-01 | delivered | 99.8 |
| 5093 | 56 | 2026-08-05 | delivered | 74.7 |
| 5094 | 53 | 2026-08-06 | delivered | 98.9 |
| 5095 | 59 | 2026-08-07 | delivered | 22.9 |
| 5096 | 18 | 2026-08-12 | delivered | 1157 |
| 5097 | 61 | 2026-08-16 | delivered | 44.7 |
| 5098 | 29 | 2026-08-20 | delivered | 105.7 |
| 5099 | 60 | 2026-08-24 | delivered | 307 |
| 5100 | 39 | 2026-08-27 | delivered | 103.9 |
| 5101 | 44 | 2026-08-28 | delivered | 243.8 |
| 5102 | 31 | 2026-08-31 | delivered | 128.9 |
| 5103 | 54 | 2026-08-31 | delivered | 298 |
| 5104 | 42 | 2026-09-01 | delivered | 388.9 |
| 5105 | 47 | 2026-09-01 | delivered | 124.7 |
| 5106 | 63 | 2026-09-06 | cancelled | 14.9 |
| 5107 | 64 | 2026-09-06 | delivered | 62.8 |
| 5108 | 65 | 2026-09-06 | cancelled | 208.6 |
| 5109 | 62 | 2026-09-11 | delivered | 34.9 |
| 5110 | 27 | 2026-09-18 | delivered | 29.9 |
| 5111 | 42 | 2026-09-20 | delivered | 104.7 |
| 5112 | 41 | 2026-09-21 | delivered | 24.9 |
| 5113 | 46 | 2026-09-27 | delivered | 94.6 |
| 5114 | 66 | 2026-09-28 | delivered | 29.9 |
| 5115 | 67 | 2026-10-01 | delivered | 34.9 |
| 5116 | 51 | 2026-10-07 | delivered | 69.8 |
| 5117 | 53 | 2026-10-07 | cancelled | 34.9 |
| 5118 | 70 | 2026-10-09 | delivered | 211.9 |
| 5119 | 64 | 2026-10-16 | delivered | 698 |
| 5120 | 68 | 2026-10-21 | delivered | 183.9 |
| 5121 | 69 | 2026-10-27 | shipped | 159.4 |
| 5122 | 27 | 2026-10-30 | shipped | 118.9 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
The tricky part is the status filter. In the ON condition it only decides which orders get attached. In WHERE it checks every finished row, and the rows for customers without orders have a NULL status. NULL <> 'cancelled' isn't true, so those rows are dropped and your LEFT JOIN behaves like an INNER JOIN.
Common mistakes
Counting cancellations
Without WHERE, the columns and row count are right but the values aren't. The exercise says "Columns and row count match, but not every value." Check the cancellation filter first.
COUNT instead of COUNT DISTINCT
COUNT(customer_id) counts every row with a customer_id, which is the number of orders again. For customers you need COUNT(DISTINCT customer_id).
Average order value from the line items
Joining orders to order_items and taking AVG(quantity * unit_price) gives you the value per line. After a join, each order appears once per line item. The exercise then only reports values that don't match, so first check whether you averaged over the line items.
Grouping by month number only
strftime('%m', order_date) lumps November 2025 and November 2026 together. In this data every month appears once, but by the second year the series would be wrong. Always use year and month.
Status filter in WHERE with a LEFT JOIN
In task 3 the number of customers then equals the number of buyers, and the exercise reports values that don't match. Move the condition into ON.
In MySQL, PostgreSQL and SQL Server
SUM, AVG, COUNT(DISTINCT …) and LEFT JOIN are written the same everywhere. The differences are in getting the month out of a date and in division:
| Database | Month as text YYYY-MM | Integer divided by integer |
|---|---|---|
| MySQL | DATE_FORMAT(order_date, '%Y-%m') | decimal result, 3/5 gives 0.6000 |
| PostgreSQL | to_char(order_date, 'YYYY-MM') | integer, the fraction is truncated |
| SQL Server | FORMAT(order_date, 'yyyy-MM') | integer, the fraction is truncated |
| SQLite (these exercises) | strftime('%Y-%m', order_date) | integer, which is why you multiply by 100.0 |
Sources: MySQL Reference Manual (Date and Time Functions, Arithmetic Operators), PostgreSQL documentation (Data Type Formatting Functions, Mathematical Functions), Microsoft Learn (FORMAT, Division).
How to check your result
- Task 1: 113 orders, revenue 16202.1, average order value 143.38, 64 customers.
- Task 2: the twelve monthly revenues add up to 16202.1 again, and the orders to 113.
- Task 3: 37 customers came through search and 32 of them bought. Customers across all channels add up to the 70 rows of the customers table.
FAQ
Why is the order value stored on the order when there are line items?
That's common in shop systems: the order header carries the total and the lines carry the detail. For per-order KPIs you use the header; for per-product KPIs you use the lines. That the two agree isn't guaranteed, though, and you'll check it in case 6.
What's the difference between COUNT(*) and COUNT(column)?
COUNT(*) counts rows, COUNT(column) only counts rows where the column isn't NULL. After a LEFT JOIN, that's the difference between "all customers" and "customers with an order".
Should I round?
For display, yes, but only at the very end. If you round intermediate results and keep calculating with them, small differences add up in the totals.
Back to the course overview.