sqlstreak

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 valuetrueNULL (unknown)
Rows WHERE keepsThe rows with NULLNone
Error?NoNo: it fails silently
Comparing two columns null-safelyIS NOT DISTINCT FROMTreats 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

namespecies

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

namespecies
Beandog
Cloverrabbit
Lunacat
Picklesdog
Pumpkincat
Tofucat

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_nullis_nullnull_safe_equal
NULLtruetrue

SQLite: 1 row

equals_nullis_nullnull_safe_equal
NULL11

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 = NULL or WHERE x <> NULL: both return nothing.

  • WHERE x != 'cat' doesn't return rows where x is NULL either: add OR x IS NULL if you want them.

  • Treating an empty string as NULL: '' is a value. Convert with NULLIF(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

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.

Start the lesson