NULL & three-valued logic
Concept  ·  SQL & Querying

NULL = NULL is UNKNOWN, not TRUE; use IS [NOT] NULL, or IS DISTINCT FROM as the null-safe comparison.

Equality with NULL is UNKNOWN, never TRUE — so NULL = NULL does not match, and a join on a nullable key silently drops the NULL pairs. IS NOT DISTINCT FROM is the NULL-safe equality that treats two NULLs as the same.

See it break
a
k
1
NULL
b
k
1
NULL
The trap
SELECT
  a.k
FROM
  a
  JOIN b ON a.k = b.k
ORDER BY
  a.k ;
The fix
SELECT
  a.k
FROM
  a
  JOIN b ON a.k IS NOT DISTINCT FROM b.k
ORDER BY
  a.k NULLS LAST;
The trap
1
The fix
1
NULL

The vendor with a NULL code never matches its twin — NULL = NULL is UNKNOWN, so the join drops it; IS NOT DISTINCT FROM treats two NULLs as equal.

The rule

Test NULLs with IS [NOT] NULL, and join on nullable keys with IS NOT DISTINCT FROM when a NULL should match a NULL.

Shows up elsewhere
WHERE keeps only TRUE
Try one →Prove it in practice →