All posts
5 min read · 2026-04-21

SQL GROUP BY mistakes explained with visuals

debugging
aggregation

GROUP BY does not throw an error when it is wrong. It just returns numbers that look right. That is what makes it dangerous.

Mistake 1: Aggregating before joining a fan-out

If you SUM(amount) and then join a child table, your sums multiply. Always aggregate the fan-out side first.

Mistake 2: Forgetting a column in the GROUP BY

Strict mode catches this. Snowflake's relaxed mode does not — it silently picks an arbitrary value for ungrouped columns.

Mistake 3: Grouping by a derived expression

Grouping by DATE_TRUNC('day', ts) and then SELECTing ts gives you the lowest ts per day, not the bucket. Group on the same expression you project.

Mistake 4: COUNT(*) on a LEFT JOIN

COUNT(*) counts the row even when the right side is NULL. Use COUNT(right_table.id) to count actual matches.

Mistake 5: Aggregating after a window function

Window functions run after GROUP BY but before ORDER BY. Mixing them confuses both you and the planner. Use a CTE.

All five of these become much easier to spot when you can see the column-level lineage of the offending view: the wrong column will be wired to the wrong source.

orders.amountorder_items.qty (1:N)daily_totals.SUM(amount)
Mistake #1 visualized: SUM(amount) is fed by both orders and order_items, multiplying the total.

Try it on your own SQL: