SQL compared
IS NULL vs = NULL in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
= NULL never matches, because comparing anything with NULL gives NULL (unknown), and WHERE only keeps rows where the condition is true. Test for missing values with IS NULL and IS NOT NULL.
Side by side
| IS NULL | = NULL | |
|---|---|---|
| Result for a NULL value | true | NULL (unknown) |
| Rows WHERE keeps | The rows with NULL | None |
| Error? | No | No: it fails silently |
| Comparing two columns null-safely | IS NOT DISTINCT FROM | Treats two NULLs as unknown |
The same data, each way
Every query below was run on PostgreSQL and SQLite when this page was built; the tables are what came back.
= NULL: no rows, no error
SELECT name, species
FROM pets
WHERE breed = NULL;PostgreSQL: no rows
| name | species |
|---|
SQLite: no rows
Try it: PostgreSQL playground · SQLite playground
IS NULL: the pets with no recorded breed
SELECT name, species
FROM pets
WHERE breed IS NULL
ORDER BY name;Result (PostgreSQL and SQLite, identical): 6 rows
| name | species |
|---|---|
| Bean | dog |
| Clover | rabbit |
| Luna | cat |
| Pickles | dog |
| Pumpkin | cat |
| Tofu | cat |
Try it: PostgreSQL playground · SQLite playground
Why: what the comparisons return
PostgreSQL shows true/false; SQLite shows 1 and 0. Either way = NULL gives NULL.
SELECT NULL = NULL AS equals_null,
NULL IS NULL AS is_null,
NULL IS NOT DISTINCT FROM NULL AS null_safe_equal;PostgreSQL: 1 row
| equals_null | is_null | null_safe_equal |
|---|---|---|
| NULL | true | true |
SQLite: 1 row
| equals_null | is_null | null_safe_equal |
|---|---|---|
| NULL | 1 | 1 |
Try it: PostgreSQL playground · SQLite playground
When to use which
- IS NULL
Always, to find missing values:
WHERE breed IS NULL,WHERE phone IS NOT NULL.- = NULL
Never in a WHERE clause. To compare two columns that may both be NULL, use
IS NOT DISTINCT FROM.
Common mistakes
WHERE x = NULLorWHERE x <> NULL: both return nothing.WHERE x != 'cat'doesn't return rows where x is NULL either: addOR x IS NULLif you want them.Treating an empty string as NULL:
''is a value. Convert withNULLIF(x, '')first.
PostgreSQL and SQLite
Both follow the standard here. SQLite prints true and false as 1 and 0, and also has the shorter null-safe operators IS and IS NOT (a IS b); IS NOT DISTINCT FROM works in both (in SQLite since 3.39).
Official documentation
Go deeper
- Guide: NULL in SQL: IS NULL, COALESCE and the traps
- Practice: SQL NULL practice: IS NULL, COALESCE and NULLIF exercises
- Lesson: Patterns and NULL (free)
Make it stick in five minutes a day
“Patterns and NULL” teaches this step by step on a real database, with instant feedback. Free, no account needed.