Testing for NULL in SQL
column = NULL never matches because NULL means unknown rather than a value, and comparisons with it evaluate to UNKNOWN; write column IS NULL or column IS NOT NULL instead.
The short answer
column = NULL never matches because NULL is not a value; it is a marker for an unknown or absent value. Any comparison with NULL evaluates to UNKNOWN, and WHERE keeps only rows for which the condition is TRUE. Write column IS NULL or column IS NOT NULL instead. For null-safe comparison, use IS DISTINCT FROM or IS NOT DISTINCT FROM where supported.
A minimal example
CREATE TABLE readings (id INTEGER PRIMARY KEY, value INTEGER);
INSERT INTO readings VALUES (1, 10), (2, NULL), (3, 30);
SELECT COUNT(*) FROM readings WHERE value = NULL; -- 0
SELECT COUNT(*) FROM readings WHERE value IS NULL; -- 1
SELECT COUNT(*) FROM readings WHERE value <> 10; -- 1
The third query returns only id = 3. The row with NULL is excluded because NULL <> 10 is UNKNOWN, not TRUE.
| Expression | Result when value is NULL |
Row kept by WHERE |
|---|---|---|
value = NULL |
UNKNOWN | No |
value IS NULL |
TRUE | Yes |
NOT (value = NULL) |
UNKNOWN | No |
value IS NOT NULL |
FALSE | No |
Why this happens
SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN. NULL means "no value" or "unknown", so it cannot equal anything, even another NULL. NULL = NULL is UNKNOWN, and NOT UNKNOWN is still UNKNOWN. WHERE keeps only TRUE, so value = NULL and its negation both return zero rows.
CHECK constraints also pass on UNKNOWN, so CHECK (value > 0) still accepts NULL unless the column is also NOT NULL.
NOT IN is a common trap: value NOT IN (10, NULL) means value <> 10 AND value <> NULL, and the second comparison is UNKNOWN for every row. Use NOT EXISTS or exclude NULLs explicitly.
What changes the answer
| Task | Standard SQL | Common alternatives |
|---|---|---|
| Test for null | value IS NULL |
None needed |
| Null-safe equality | a IS NOT DISTINCT FROM b |
MySQL a <=> b; SQLite a IS b |
| Null-safe inequality | a IS DISTINCT FROM b |
SQLite a IS NOT b |
| Replace null for display | COALESCE(value, 0) |
IFNULL in MySQL and SQLite; NVL in Oracle |
IS DISTINCT FROM is standard SQL:1999, supported by PostgreSQL, SQLite 3.39 and later, and SQL Server 2022 and later; databases without it, including older Oracle releases, need a CASE expression or vendor function. COALESCE changes semantics, so use it for presentation or to treat absence as a default, not as a substitute for IS NULL in predicates.
Aggregates also treat NULL specially: COUNT(*) counts rows, COUNT(value) counts non-null values, and SUM, AVG, MIN, and MAX ignore NULLs. SUM over an empty or all-NULL set returns NULL. GROUP BY and DISTINCT treat NULLs as equal for grouping, even though = does not.
Edge cases
NULL OR TRUEis TRUE andNULL AND FALSEis FALSE, because the result is known even with an unknown operand.- Arithmetic and most string concatenation with NULL yield NULL; MySQL's
CONCATreturns NULL if any argument is NULL. - A foreign key column that is NULL does not require a matching parent row under the default MATCH SIMPLE rule.
- SQL Server's
SET ANSI_NULLS OFFis deprecated; modern versions always use standard NULL comparison behavior. - A parameterized query such as
WHERE value = ?still does not match when the parameter is NULL. UseIS NULLor build the predicate conditionally.
Practical checks
- Search the codebase for
= NULL,<> NULL, and!= NULL; replace them withIS NULLandIS NOT NULL. - Add
NOT NULLconstraints where absence is invalid, so UNKNOWN cannot hide missing data. - Test with fixtures that contain NULLs in every nullable column, and assert row counts for filters and aggregates.
- When comparing two nullable columns, choose an explicit null-safe operator rather than adding
OR both_are_nullconditions. - For the underlying model, see the relational model.
Sources
Revision history
- Published
- Updated
- — Initial publication.