SQL NULL Traps: NOT IN, COUNT and AVG Explained

By ··7 min read

Short answer

In SQL, NULL means "unknown". It is not zero and it is not an empty string. Any comparison with NULL returns unknown, and WHERE keeps only the rows where the condition is true. This is why NOT IN returns nothing when the list contains a NULL, why = NULL never matches, and why AVG and COUNT(column) skip NULL values.

In the earlier posts we filtered, joined and counted rows without asking what is inside the columns. Real tables are rarely complete. On a table with a million customers, a phone number is missing for thousands of them, and each of those gaps is stored as NULL. Because NULL means unknown, SQL treats it differently from every other value, and four common queries change their answer as a result.

Quick reference

ExpressionResultWhy
NULL = NULLunknown, not trueTwo unknowns cannot be called equal
col IS NULLtrue when col is NULLThe correct test
5 > NULLunknownComparison with an unknown value
1 NOT IN (2, NULL)unknown, so the row is excluded1 <> NULL is unknown
COALESCE(col, 0)col, or 0 if col is NULLReplaces NULL

Trap 1: NOT IN with a NULL in the list

To find customers with no orders, we can write NOT IN. The expression x NOT IN (a, b, NULL) is the same as x <> a AND x <> b AND x <> NULL. The last part is unknown, so the whole condition can never be true. As a result, one NULL in orders.customer_id is enough to empty the answer.

-- WRONG: returns 0 rows if any orders.customer_id is NULL
SELECT * FROM customers
WHERE customer_id NOT IN (SELECT customer_id FROM orders);

-- right
SELECT * FROM customers c
WHERE NOT EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);

Another way is to filter the NULL values out of the subquery. NOT EXISTS is usually the safer habit, since it keeps working when the data changes.

Trap 2: = NULL never matches

To find the missing values, we test with IS NULL. A comparison with = returns unknown, and WHERE only keeps rows that are true.

SELECT * FROM users WHERE phone = NULL;   -- WRONG: always empty
SELECT * FROM users WHERE phone IS NULL;  -- right

Trap 3: <> also drops the NULL rows

WHERE status <> 'cancelled' does not return rows where status is NULL. If a missing status should count as "not cancelled", we write status IS DISTINCT FROM 'cancelled' in PostgreSQL. In databases without that operator, status <> 'cancelled' OR status IS NULL gives the same result.

Trap 4: aggregates skip NULL

Most aggregate functions ignore NULL, with COUNT(*) as the exception. Take three test scores, 10, NULL and 20.

-- scores: 10, NULL, 20
SELECT COUNT(*)                 AS all_rows,   -- 3
       COUNT(score)             AS non_null,   -- 2
       AVG(score)               AS avg_skip,   -- 15
       AVG(COALESCE(score, 0))  AS avg_zero    -- 10
FROM results;

Neither average is automatically right. AVG skips the missing score, so 15 is the average of the people who took the test. Replacing it with 0 gives 10, which is the average if a missing score really means zero. Before choosing, we decide which question the report is answering and say so in the report.

SUM over a group where every value is NULL returns NULL, not 0. When a report needs a number, we write COALESCE(SUM(x), 0).

A habit that prevents most NULL bugs

  1. Before filtering or joining, run SELECT COUNT(*) - COUNT(col) to see how many NULL values the column has.
  2. Write down what NULL means in that column, for example not collected, not applicable or zero.
  3. Prefer NOT EXISTS over NOT IN, and IS NULL over = NULL.

Frequently asked questions

Why does NOT IN return no rows in SQL?

If the list or subquery contains a NULL, every NOT IN comparison becomes unknown, and WHERE only keeps rows that are true. Use NOT EXISTS or remove NULLs from the subquery.

What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts every row. COUNT(column) counts only rows where that column is not NULL.

Should I replace NULL with 0?

Only when a missing value really means zero. For averages and rates, replacing NULL with 0 changes the meaning, so decide first what the missing value represents.

References

Run these queries yourself.

Predict the result, run it, and check the row count. That habit is what Practice trains.