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
| Expression | Result | Why |
|---|---|---|
| NULL = NULL | unknown, not true | Two unknowns cannot be called equal |
| col IS NULL | true when col is NULL | The correct test |
| 5 > NULL | unknown | Comparison with an unknown value |
| 1 NOT IN (2, NULL) | unknown, so the row is excluded | 1 <> NULL is unknown |
| COALESCE(col, 0) | col, or 0 if col is NULL | Replaces 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; -- rightTrap 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
- Before filtering or joining, run
SELECT COUNT(*) - COUNT(col)to see how manyNULLvalues the column has. - Write down what
NULLmeans in that column, for example not collected, not applicable or zero. - Prefer
NOT EXISTSoverNOT IN, andIS NULLover= NULL.


