Aggregation After Joins and NULL Handling

Counting, summing, pre-aggregating, and handling NULLs correctly after one-to-many and outer joins.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Aggregation after joins

Avoid wrong counts and duplicated totals after joining.

Use COUNT(DISTINCT ...) when joins multiply rows

Protect aggregates from one-to-many duplication.

sqlANYaggregationdistinct
sql
SELECT c.customer_id,
       COUNT(DISTINCT o.order_id) AS order_count
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id;
Notes

A one-to-many join can inflate counts unless you count distinct entity keys.

Pre-aggregate before joining

Summarize large fact tables first, then join the result.

sqlANYcteaggregation
sql
WITH order_totals AS (
  SELECT customer_id, SUM(total_amount) AS total_spent
  FROM orders
  GROUP BY customer_id
)
SELECT c.customer_id, c.name, ot.total_spent
FROM customers c
LEFT JOIN order_totals ot ON ot.customer_id = c.customer_id;
Notes

This often improves both correctness and performance.

Use COALESCE for outer-join aggregates

Replace `NULL` with a friendlier value in results.

sqlANYcoalescenulls
sql
SELECT c.customer_id,
       COALESCE(SUM(o.total_amount), 0) AS total_spent
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
GROUP BY c.customer_id;
Notes

Outer joins can produce `NULL` totals for unmatched rows. `COALESCE` turns them into `0` or another default.

NULL behavior

Understand how NULL affects output and predicates.

Expect NULLs on the optional side of outer joins

Missing matches appear as NULL-valued columns.

textANYnullsouter-joins
text
LEFT JOIN → right-side columns may be NULL
RIGHT JOIN → left-side columns may be NULL
FULL OUTER JOIN → either side may be NULL
Notes

This is normal and often what makes outer joins useful.

Recommended next

No recommendations yet.