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.
Code
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
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.JSON Diff ViewerCompare two JSON documents and list every added, removed, and changed path in a structural diff, with clear before and after values.
FAQ
- What are the relations in play here?
- The references are `user_migrations`, `current_email`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.