What is NULL and how does it differ from 0 and an empty string?
NULL means the value is unknown or missing, and it is neither zero nor an empty string. A comparison with NULL yields UNKNOWN (three-valued logic), and WHERE, JOIN ON and HAVING keep only TRUE, so such rows silently drop out. Arithmetic with NULL gives NULL, aggregates skip it, and NULL is tested with IS NULL. COALESCE substitutes a value.
SQL · Basics
The filter "balance is not 0" returns one account out of three. Where did the third go?
Three accounts: the first has 100, the second 0, the third has a balance that has not been calculated. The query WHERE balance <> 0 returns only the first, although "not zero" is obviously true for the third as well. The tester sees a mismatch between the query and the report on the page. The cause is the special behaviour of NULL, and it is behind many "vanished" rows and wrong sums.
How it works and why. NULL means "the value is unknown or absent". It is neither zero nor an empty string:
Three different "nothings"
Because of that, comparisons with NULL follow three-valued logic: the result can be TRUE, FALSE or UNKNOWN. balance <> 0 for NULL is UNKNOWN, not TRUE. WHERE filters, JOIN ... ON conditions and HAVING keep only rows where the condition is TRUE, so UNKNOWN is dropped silently, just like FALSE. Even NULL = NULL gives UNKNOWN, and arithmetic with NULL returns NULL: 100 + NULL is NULL.
Example: from a lost row to the right filter. The data is:
A filter that drops NULL
| id | balance |
|---|---|
| 1 | 100 |
| 2 | 0 |
| 3 | NULL |
| id | balance |
|---|---|
| 1 | 100 |
| 2 | 0 filtered out |
| 3 | NULL lost |
The fix depends on the requirement. If you need "everything except a known zero", write the condition explicitly:
-- idea: all accounts with a non-zero balance
SELECT id FROM accounts WHERE balance <> 0;
-- returns only id = 1
SELECT id FROM accounts
WHERE balance IS DISTINCT FROM 0;
-- returns id = 1 and id = 3
IS NULL and IS NOT NULL check exactly the absence of a value, while IS DISTINCT FROM compares in a way that treats NULL as an ordinary value. More on searching for empty values is in the lesson about IS NULL.
Aggregates and NULL. The functions COUNT(col), SUM, AVG skip NULL, while COUNT(*) counts all rows. So the average is computed only over known values (here 50, not 33), and the sum of an empty group is NULL, not 0. To replace NULL with a value use COALESCE:
SELECT COUNT(*), COUNT(balance), -- 3, 2
SUM(balance), AVG(balance) -- 100, 50
FROM accounts;
SELECT SUM(balance) FROM accounts WHERE id = 3; -- NULL
SELECT COALESCE(SUM(balance), 0) FROM accounts
WHERE id = 3; -- 0
Other places where NULL surprises: NOT IN with a list that contains NULL returns no rows (a separate lesson); a CHECK (balance >= 0) constraint lets NULL through, because UNKNOWN is not treated as a violation, so mandatory fields need NOT NULL as well; in a UNIQUE column several NULLs are allowed in PostgreSQL (since version 15 there is UNIQUE NULLS NOT DISTINCT); GROUP BY and DISTINCT gather all NULLs into one group. Sorting: in PostgreSQL NULL goes last in ascending order, in some DBMS first.
Limits and dialects. Oracle treats the empty string '' as NULL ('' IS NULL is TRUE), PostgreSQL does not. A JSON API response also has three states: the field is absent, the field is null and an empty string, and they correspond to different situations in the database. For QA that means separate cases: the field was not sent, null was sent, an empty string was sent.
Common mistakes.
- Writing
= NULLor<> NULL: the result is always UNKNOWN, no rows come back. - Filtering with
<>orNOTand not noticing that NULL rows dropped out. - Treating average and sum as computed "over all rows": NULL is skipped.
- Relying on
CHECKinstead ofNOT NULLfor mandatory fields. - Mixing up NULL, 0 and an empty string in test expectations.
Check yourself
Will WHERE phone = NULL return the rows where the phone is not set (NULL)?
Answer
No. A comparison with NULL gives UNKNOWN, so no row passes. The correct form is WHERE phone IS NULL.
In a group all balance values are NULL. What do SUM(balance), COUNT(balance) and COUNT(*) return?
Answer
SUM returns NULL (not 0), COUNT(balance) gives 0 because NULLs are skipped, and COUNT(*) counts all rows of the group. To get 0 in a report use COALESCE(SUM(balance), 0).
<> loses NULL rows, CHECK lets them through, aggregates ignore them, and NOT IN with a NULL in the list returns nothing. Mandatory fields need NOT NULL, and in tests I separately check a missing field, null and an empty string.