0Pricing
SQL Academy · Lesson

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

  1. COUNT, SUM, AVG, MIN, MAX
  2. GROUP BY Single and Multiple Columns
  3. HAVING vs WHERE
  4. Common Pitfalls: NULLs in Aggregates
← Back to SQL Academy