Project: a monthly report
Split a long query into steps in WITH: calendar → paid orders → basic monthly numbers → each person's first order month → new customers per month → build the report with a calendar LEFT JOIN → add LAG on the outside for month-over-month change. You can run each step on its own to check it.
You've been through a whole year of data, and the boss says: "Give me a monthly business report: how much we sold each month, how many people bought, how many new customers, how many returning ones, and how it compares with last month. And tell me what you see in it." This lesson ties together everything from the last 11 lessons to write that report.
First, work out what you need
In this step you don't write any code yet. First list every column of the report, and where each one comes from:
| Column | Meaning | Uses |
|---|---|---|
month | The month, all 12 with none missing | Lesson 10: generate a calendar with recursion |
orders, sales | Number of paid orders, sales | Lesson 1: only count paid orders; lesson 2: group by month |
buyers | How many people bought this month | Lesson 2: COUNT(DISTINCT user_id) |
new_buyers | How many people bought for the first time this month | Lesson 8: first order month |
returning | Returning customers: buyers − new customers | Subtraction |
vs_last | How much sales went up on last month | Lesson 6: LAG |
That's a lot for one query to work out, and written as one big lump it's hard to debug. The answer is to go step by step in WITH: each step does just one thing and gets a name.
Put these steps in an order that runs: later steps use the results of earlier ones.
newbies AS (SELECT first_month AS month_start, COUNT(*) AS new_buyers FROM firsts GROUP BY first_month)WITH paid AS (SELECT user_id, amount, DATE_SUB(order_date, INTERVAL DAY(order_date) - 1 DAY) AS month_start FROM orders WHERE status = 'paid'),SELECT DATE_FORMAT(month_start, '%Y-%m') AS month, new_buyers FROM newbies ORDER BY month_start LIMIT 3;firsts AS (SELECT user_id, MIN(month_start) AS first_month FROM paid GROUP BY user_id),
month | new_buyers 2025-01 | 1 2025-03 | 6 2025-05 | 5
firsts uses paid, and newbies uses firsts, so the order is paid → firsts → newbies, and last comes the SELECT that uses newbies. Notice that February and April are missing from the result again: those months had no new customers. The next step fills them in with a calendar.The calendar and the basic monthly numbers
In this step you write the first three steps: the calendar months, the paid orders paid (moving each date back to the 1st while you're at it), and the basic monthly numbers basics. Finally, LEFT JOIN the basics onto the calendar, so months with no orders get 0:
1WITH RECURSIVE months AS (2 SELECT DATE('2025-01-01') AS month_start3 UNION ALL4 SELECT DATE_ADD(month_start, INTERVAL 1 MONTH)5 FROM months6 WHERE month_start < '2025-12-01'7),8paid AS (9 SELECT user_id, amount,10 DATE_SUB(order_date, INTERVAL DAY(order_date) - 1 DAY) AS month_start11 FROM orders12 WHERE status = 'paid'13),14basics AS (15 SELECT month_start, COUNT(*) AS orders, SUM(amount) AS sales,16 COUNT(DISTINCT user_id) AS buyers17 FROM paid18 GROUP BY month_start19)20SELECT DATE_FORMAT(m.month_start, '%Y-%m') AS month,21 COALESCE(b.orders, 0) AS orders,22 COALESCE(b.sales, 0) AS sales,23 COALESCE(b.buyers, 0) AS buyers24FROM months AS m25LEFT JOIN basics AS b ON b.month_start = m.month_start26ORDER BY m.month_start;
| month | orders | sales | buyers |
|---|---|---|---|
| 2025-01 | 1 | 36 | 1 |
| 2025-02 | 0 | 0 | 0 |
| 2025-03 | 7 | 366 | 7 |
| 2025-04 | 2 | 126 | 2 |
| 2025-05 | 11 | 743 | 10 |
| 2025-06 | 5 | 310 | 5 |
| 2025-07 | 13 | 820 | 12 |
| 2025-08 | 11 | 626 | 10 |
| 2025-09 | 16 | 977 | 13 |
| 2025-10 | 12 | 789 | 11 |
| 2025-11 | 21 | 1042 | 18 |
| 2025-12 | 16 | 588 | 15 |
Note that WITH RECURSIVE is written only once, right at the start. The steps that aren't recursive, paid and basics, can still follow it.
If you change LEFT JOIN to JOIN, which row goes missing?
JOIN only keeps rows that match on both sides. basics has no February, so February gets dropped, and you're back to the old problem from lesson 2. With the calendar on the left and a LEFT JOIN, every month is sure to be there.New and returning customers
In this step you add the first order month from lesson 8: firsts is the month each person first bought, and newbies counts how many new customers each month has. Among this month's buyers, anyone who isn't new is a returning customer.
New and returning customers in the last four months
✓Add new and returning customers to the report, for September to December only. Four columns: month, buyers, new_buyers and returning (buyers minus new customers), sorted by month.
| month | buyers | new_buyers | returning |
|---|---|---|---|
| 2025-09 | 13 | 2 | 11 |
| 2025-10 | 11 | 5 | 6 |
| 2025-11 | 18 | 2 | 16 |
| 2025-12 | 15 | 1 | 14 |
1WITH RECURSIVE months AS (2 SELECT DATE('2025-01-01') AS month_start3 UNION ALL4 SELECT DATE_ADD(month_start, INTERVAL 1 MONTH)5 FROM months6 WHERE month_start < '2025-12-01'7),8paid AS (9 SELECT user_id, amount,10 DATE_SUB(order_date, INTERVAL DAY(order_date) - 1 DAY) AS month_start11 FROM orders12 WHERE status = 'paid'13),14basics AS (15 SELECT month_start, COUNT(DISTINCT user_id) AS buyers16 FROM paid17 GROUP BY month_start18),19firsts AS (20 SELECT user_id, MIN(month_start) AS first_month21 FROM paid22 GROUP BY user_id23),24newbies AS (25 SELECT first_month AS month_start, COUNT(*) AS new_buyers26 FROM firsts27 GROUP BY first_month28)29SELECT DATE_FORMAT(m.month_start, '%Y-%m') AS month,30 COALESCE(b.buyers, 0) AS buyers31FROM months AS m32LEFT JOIN basics AS b ON b.month_start = m.month_start33WHERE m.month_start >= '2025-09-01'34ORDER BY m.month_start;
The whole report
In this step you put all the steps together. Every column is filled in first inside report, and then LAG on the outside works out the month-over-month change: once the calendar has filled the gaps, the previous row is always last month.
1WITH RECURSIVE months AS (2 SELECT DATE('2025-01-01') AS month_start3 UNION ALL4 SELECT DATE_ADD(month_start, INTERVAL 1 MONTH)5 FROM months6 WHERE month_start < '2025-12-01'7),8paid AS (9 SELECT user_id, amount,10 DATE_SUB(order_date, INTERVAL DAY(order_date) - 1 DAY) AS month_start11 FROM orders12 WHERE status = 'paid'13),14basics AS (15 SELECT month_start, COUNT(*) AS orders, SUM(amount) AS sales,16 COUNT(DISTINCT user_id) AS buyers17 FROM paid18 GROUP BY month_start19),20firsts AS (21 SELECT user_id, MIN(month_start) AS first_month22 FROM paid23 GROUP BY user_id24),25newbies AS (26 SELECT first_month AS month_start, COUNT(*) AS new_buyers27 FROM firsts28 GROUP BY first_month29),30report AS (31 SELECT m.month_start,32 COALESCE(b.orders, 0) AS orders,33 COALESCE(b.sales, 0) AS sales,34 COALESCE(b.buyers, 0) AS buyers,35 COALESCE(n.new_buyers, 0) AS new_buyers36 FROM months AS m37 LEFT JOIN basics AS b ON b.month_start = m.month_start38 LEFT JOIN newbies AS n ON n.month_start = m.month_start39)40SELECT DATE_FORMAT(month_start, '%Y-%m') AS month,41 orders, sales, buyers, new_buyers,42 buyers - new_buyers AS returning,43 sales - LAG(sales) OVER (ORDER BY month_start) AS vs_last44FROM report45ORDER BY month_start;
| month | orders | sales | buyers | new_buyers | returning | vs_last |
|---|---|---|---|---|---|---|
| 2025-01 | 1 | 36 | 1 | 1 | 0 | NULL |
| 2025-02 | 0 | 0 | 0 | 0 | 0 | -36 |
| 2025-03 | 7 | 366 | 7 | 6 | 1 | 366 |
| 2025-04 | 2 | 126 | 2 | 0 | 2 | -240 |
| 2025-05 | 11 | 743 | 10 | 5 | 5 | 617 |
| 2025-06 | 5 | 310 | 5 | 2 | 3 | -433 |
| 2025-07 | 13 | 820 | 12 | 5 | 7 | 510 |
| 2025-08 | 11 | 626 | 10 | 2 | 8 | -194 |
| 2025-09 | 16 | 977 | 13 | 2 | 11 | 351 |
| 2025-10 | 12 | 789 | 11 | 5 | 6 | -188 |
| 2025-11 | 21 | 1042 | 18 | 2 | 16 | 253 |
| 2025-12 | 16 | 588 | 15 | 1 | 14 | -454 |
A complete monthly report: 12 rows, and you can explain how every column was worked out.
What's March's vs_last?
Conclusions for the boss
This step is the last and most important part of data analysis: saying what the numbers mean. When you hand in the report, add a few sentences:
- Business is getting better, mostly thanks to returning customers. The second half of the year sold 4842, more than 3 times the 1581 of the first half. November had 18 buyers, and 16 of them were returning customers.
- Winning new customers has slowed down lately. There were 6 new customers in March and 5 each in May, July and October, but only 2 in November and 1 in December. Returning customers won't stay forever, so the shop needs a plan to bring in new ones.
- Don't jump to conclusions about December's drop. November had the big sales, and December's data only goes up to the 26th.
Also say how the numbers were counted and what's wrong with the data (the health check from lesson 1): only paid orders are counted, with cancelled, refunded and test orders left out; Elena may have signed up twice; and 2 users didn't fill in their city.
Which of these can you not conclude from this report?
What you learned
- Before writing a report, list every column and how it's worked out, then start.
- Split a long query into steps in
WITH. Each step does just one thing and can be run on its own to check it.WITH RECURSIVEonly needs to be written once, at the very start. - Put the calendar on the left,
LEFT JOINall the numbers, fill in 0 withCOALESCE, and work outLAGlast. - New customers come from the first order month; buyers minus new customers are the returning customers.
- Conclusions should start from the numbers and say how they were counted and what's wrong with the data. Don't write a guess as the reason.
You've finished the whole course! Next you can try the "SQL interview questions", where retention, login streaks and top N per group are all things this course has covered.