23/30
Part 5 of 5 · The worked exampleLearn

A worked example

The question, read back and answered by hand

Here's one generated query, debugged from start to finish with the routine. The question:

For each city, March revenue in dollars and dishes sold. List every city, with zeros where there were no March orders.

Read it back. One row per city, so three rows: Austin, Boston, Denver. Revenue counts each March order once. Dishes add up item quantities. March runs from 2026-03-01 00:00 up to, not including, 2026-04-01.

Answer it by hand. Austin's March orders are 10, 11 and 12: 2500 + 1800 + 1800 = 6100 cents, and 1 + 2 + 3 + 1 = 7 dishes. Boston's only March order is 13, late on the 31st: 1500 cents and 1 dish. Order 14 is April. Denver has nothing.

cityrevenuedishes
Austin61.07
Boston15.01
Denver0.00

Read in run order, the generated query has three suspects before it runs

The generated query:

SELECT c.city,
       SUM(o.total_cents) / 100.0 AS revenue,
       SUM(i.qty) AS dishes
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
LEFT JOIN items i ON i.order_id = o.order_id
WHERE o.placed_at BETWEEN '2026-03-01' AND '2026-03-31'
GROUP BY c.city
ORDER BY c.city;

Explain it in run order. Say what one row is after each step:

RunsClauseOne row after it is
1FROM customers ca customer
1LEFT JOIN orders oan order, or a row of NULLs for a customer with none
1LEFT JOIN items ian item line, so order 10 and its 2500 appear twice
2WHERE placed_at BETWEEN '2026-03-01' AND '2026-03-31'a row in the range: the NULL rows can't pass, and the range ends at a bare date
3GROUP BY c.citya city
4SUM(o.total_cents) / 100.0a city, with each total summed once per item line

That's three suspects before you run anything: a fan-out, a WHERE that undoes the LEFT JOIN, and a boundary.

Predict

Run the generated query on the fixture. How many of the three cities come back?

Pick an answer above to see the rest