Null-Safe Comparison With IS DISTINCT FROM

Compare two nullable columns without nulls silently dropping rows from a WHERE clause, using IS DISTINCT FROM instead of not-equal.

SqlData Integrity

Code

sqlsnippet
11 lines
SELECT
    id,
    legacy_email,
    current_email
FROM user_migrations
WHERE legacy_email IS DISTINCT FROM current_email
  AND resolved_at IS NULL
ORDER BY id;

-- Using legacy_email <> current_email instead would silently exclude every
-- row where either side is NULL, which is exactly the mismatch we want.

What the code does

What runs is SELECT, with user_migrations and current_email as the object involved.

Mechanically, the statements are built from: a WHERE clause that narrows down the rows one statement touches; an ORDER BY that fixes the output order; a DISTINCT qualifier that collapses duplicate rows.

Applying it

The user_migrations table is the working surface here.

Statements that matter

SELECT
    id,
    legacy_email,
    current_email
FROM user_migrations
WHERE legacy_email IS DISTINCT FROM current_email
  AND resolved_at IS NULL
ORDER BY id;

Interactive Tools for This Topic

FAQ

What are the relations in play here?
The references are `user_migrations`, `current_email`.