Practice Discrete Math

Relations / Sql Aggregation Nulls

Least You Need to Know: GROUP BY, Aggregation, and NULL Semantics

Aggregation questions are about partitioning rows into groups, then summarizing each group carefully. The common traps are filtering at the wrong stage, forgetting how `NULL` behaves, and accidentally duplicating rows before aggregating.

The least you need to know

Key notation

GROUP BY k partition rows by key k before aggregating
COUNT(*) count every row in the group
COUNT(col) count only rows where col is not NULL

Tiny worked example

  • To count orders per customer, group order rows by `customer_id` and compute `COUNT(*)` in each group.
  • If you only want customers with at least three orders, that threshold applies after grouping, so it belongs in `HAVING`.
  • If a nullable coupon column is counted with `COUNT(coupon_code)`, rows whose coupon is `NULL` do not contribute.
  • If you join orders to order_items before counting orders, each order can duplicate into many item rows, so the aggregate may need `COUNT(DISTINCT order_id)` instead.

Common mistakes

How to recognize this kind of problem

Start practice