How do you find rows where the value is NULL?
Rows where the value is NULL are found with IS NULL, because a comparison with NULL always gives UNKNOWN. So WHERE phone = NULL returns zero rows without an error. The reverse trap: not-equal (<>) and NOT IN also drop rows with NULL, so they are added explicitly with OR IS NULL or IS DISTINCT FROM is used. COALESCE supplies a default value.
SQL · SELECT and WHERE
WHERE phone = NULL returned 0 rows although thousands of phones are empty. Did the database lie?
A tester looks for users without a phone: WHERE phone = NULL. The query runs without an error and returns zero rows. There are two conclusions: "there are no such users" (wrong) or "the database is broken" (also wrong). This lesson explains what NULL means in query logic, why a comparison with it is never true and where two reverse traps hide.
How it works and why. NULL means "the value is unknown or missing", not "zero" or "an empty string" (the type basics lesson covers the difference). That is why SQL uses three-valued logic: any comparison can give true, false or "unknown". A comparison with NULL always gives "unknown": asking whether an unknown value equals 5, or equals another unknown, has no answer. The WHERE filter keeps only true rows, so "unknown" is dropped just like false. The logical operators also work in three values:
| Expression | Result | Why |
|---|---|---|
NULL = NULL | unknown | unknown equals unknown: cannot tell |
TRUE AND NULL | unknown | depends on the unknown |
FALSE AND NULL | false | already known that AND cannot be true |
TRUE OR NULL | true | already known that OR is true |
NOT NULL | unknown | the negation of unknown stays unknown |
For testing emptiness there are separate predicates that always answer true or false: IS NULL and IS NOT NULL. For a comparison "does the value differ, taking NULL into account" PostgreSQL has IS DISTINCT FROM (and MySQL's equivalent for equality is <=>).
SELECT * FROM users WHERE phone IS NULL; -- correct
SELECT * FROM users WHERE phone = NULL; -- always 0 rows, no error
Example: a row that "disappeared". The second trap mirrors the first. The condition "status is not failed" looks like a full complement of "status is failed", but rows with a NULL status fall into neither selection: in the first the status is unknown, in the second as well.
A row with a NULL status vanishes from both sides
| id | status |
|---|---|
| 1 | completed |
| 2 | pending |
| 3 | NULL lost |
| id | status |
|---|---|
| 1 | completed |
| 2 | pending |
| 3 | NULL |
The fix is to mention NULL explicitly or use IS DISTINCT FROM:
-- everything except failed, including NULL
SELECT id, status FROM transactions
WHERE status IS DISTINCT FROM 'failed';
-- the same without IS DISTINCT FROM
SELECT id, status FROM transactions
WHERE status <> 'failed' OR status IS NULL;
Limits and alternatives. The same principle holds wherever a comparison appears. NOT (status = 'failed') is also unknown for NULL and is dropped. An operation on NULL gives NULL: NULL + 1, concatenation ('a' || NULL). To substitute a default there is COALESCE(x, 0) (the lesson on aggregates with NULL), and for the reverse NULLIF(a, b), which returns NULL when the values are equal. In some constructs NULL behaves "like a value": DISTINCT and GROUP BY treat all NULLs as the same and give one row or group. And the UNIQUE constraint in PostgreSQL allows several NULLs in a column because they are "not equal"; since PostgreSQL 15 this can be changed with UNIQUE NULLS NOT DISTINCT. This inconsistency exists: when checking a specific database, do not guess, check with a query. Also remember that an empty string and NULL are different values, except in Oracle, where an empty string is stored as NULL.
-- both NULL and an empty string can mean "empty"
SELECT id FROM users
WHERE phone IS NULL OR phone = '';
-- protection against division by zero: NULLIF gives NULL
SELECT total / NULLIF(items, 0) FROM orders;
Common mistakes.
= NULLor<> NULLinstead ofIS NULLandIS NOT NULL.<>orNOT INon a column that has NULLs: those rows silently disappear.- Searching for "empty" values only by NULL or only by
''. - Assuming UNIQUE forbids several NULLs (in PostgreSQL by default it does not).
- Carrying a conclusion about NULL from one database to another (Oracle, empty string).
Check yourself
What does SELECT 1 WHERE NULL = NULL return?
Answer
Zero rows. NULL = NULL gives unknown, and WHERE keeps only true. To test for NULL you need IS NULL, and to compare taking NULL into account IS NOT DISTINCT FROM.
A table has 100 transactions: 20 failed, 70 completed, 10 with status NULL. How many does WHERE status <> 'failed' return?
Answer
70. The rows with NULL are dropped because the comparison gives unknown. To get 80 you need to add OR status IS NULL or write status IS DISTINCT FROM 'failed'.
Why should a tester look for empty fields both through IS NULL and through = ''?
Answer
Data can arrive by different paths: a form sends an empty string, an API a missing field. The column will hold both variants, and checking only one misses bugs. The correct application behaviour (what counts as empty) is clarified in the requirements.