SQL
5 min read
BEGINNER
NULL-safe comparison
Quick AnswerIS NOT DISTINCT FROM compares values while treating two NULLs as equivalent. IS DISTINCT FROM is its inverse. Both produce definite truth values for scalar operands.
Core idea
IS NOT DISTINCT FROM compares values while treating two NULLs as equivalent. IS DISTINCT FROM is its inverse. Both produce definite truth values for scalar operands.
Minimal example
sql
SELECT NULL IS NOT DISTINCT FROM NULL, NULL IS DISTINCT FROM 0;Expected SQLite rows:
text
1 | 1How it works
For ordinary comparable scalar values, equal non-NULL values or two NULLs are not distinct; one NULL and one non-NULL value are distinct. PostgreSQL and SQLite 3.46 support this spelling; MySQL uses <=> for null-safe equality. Other versions need separate verification.
Practical example
sql
SELECT id FROM sort_scores WHERE score IS DISTINCT FROM 10 ORDER BY id;Expected SQLite rows:
text
234Common mistake and practice
Use null-safe comparison when checking whether an optional field changed. Predict whether NULL and 0 are distinct.