24/30
Part 5 of 5 · The worked exampleLearn
The rows under the GROUP BY show the extra 2500 and every row WHERE drops
Look at the rows the GROUP BY works on, before the WHERE gets to them. The last column is the WHERE's condition on each row:
SELECT c.city, c.name, o.order_id, o.placed_at, o.total_cents, i.qty,
o.placed_at BETWEEN '2026-03-01' AND '2026-03-31' AS in_range
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
ORDER BY c.customer_id, o.order_id, i.dish;
| city | name | order_id | placed_at | total_cents | qty | in_range |
|---|---|---|---|---|---|---|
| Austin | Ana | 10 | 2026-03-02 12:15 | 2500 | 1 | 1 |
| Austin | Ana | 10 | 2026-03-02 12:15 | 2500 | 2 | 1 |
| Austin | Ana | 11 | 2026-03-02 19:40 | 1800 | 3 | 1 |
| Austin | Ben | 12 | 2026-03-15 08:05 | 1800 | 1 | 1 |
| Boston | Cy | 13 | 2026-03-31 20:30 | 1500 | 1 | 0 |
| Boston | Cy | 14 | 2026-04-01 09:00 | 2100 | 2 | 0 |
| Denver | Dee | NULL | NULL | NULL | NULL | NULL |
Seven rows from four customers and five orders. Order 10's 2500 appears twice: that's the extra $25.00.
The WHERE keeps the four rows with a 1. It drops Cy's 20:30 order on the boundary, and Cy's April order, as it should. Dee's row gets NULL, so it goes too, and takes Denver with it.