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.
| city | revenue | dishes |
|---|---|---|
| Austin | 61.0 | 7 |
| Boston | 15.0 | 1 |
| Denver | 0.0 | 0 |
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:
| Runs | Clause | One row after it is |
|---|---|---|
| 1 | FROM customers c | a customer |
| 1 | LEFT JOIN orders o | an order, or a row of NULLs for a customer with none |
| 1 | LEFT JOIN items i | an item line, so order 10 and its 2500 appear twice |
| 2 | WHERE 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 |
| 3 | GROUP BY c.city | a city |
| 4 | SUM(o.total_cents) / 100.0 | a 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.
Run the generated query on the fixture. How many of the three cities come back?