Common Pitfalls: NULLs in Aggregates
Avoid the classic NULL traps: AVG ignoring NULLs, COUNT(column) skipping NULLs, and surprises when grouping on nullable columns.
Aggregates Skip NULL
Every aggregate except COUNT(*) ignores NULL inputs. This is occasionally what you want — and sometimes a bug.
Classic Trap: COUNT(*) vs COUNT(col)
Different results when the column has NULLs:
SELECT COUNT(*) FROM users; -- 1000
SELECT COUNT(email) FROM users; -- 950 (50 users have NULL email)All lessons in this course
- COUNT, SUM, AVG, MIN, MAX
- GROUP BY Single and Multiple Columns
- HAVING vs WHERE
- Common Pitfalls: NULLs in Aggregates