SQL Server
SELECT * FROM tasks WHERE assignee IS NOT DISTINCT FROM @v;
SQL Server 2022+. On older versions write (assignee = @v OR (assignee IS NULL AND @v IS NULL)).
NULL = NULL is unknown, not true, unless you use a null-safe operator.
Filed under Querying
IS NOT DISTINCT FROM is the standard and now runs on three of the five engines; MySQL's <=> and SQLite's IS are fine local spellings. Oracle is the holdout: use DECODE or the paired (a = b OR (a IS NULL AND b IS NULL)) test, which works on every engine.
Ordinary = returns unknown when either side is NULL, so NULLs never match. The SQL standard answer is IS NOT DISTINCT FROM, which SQL Server (2022+), PostgreSQL and SQLite (3.39+) support. MySQL has <=>, SQLite also has IS, and Oracle needs a workaround.
SELECT * FROM tasks WHERE assignee IS NOT DISTINCT FROM @v;
SQL Server 2022+. On older versions write (assignee = @v OR (assignee IS NULL AND @v IS NULL)).
SELECT * FROM tasks WHERE assignee IS NOT DISTINCT FROM $1;
The negation is IS DISTINCT FROM. $1 is a bind parameter.
SELECT * FROM tasks WHERE assignee <=> @v;
<=> returns 1 for NULL <=> NULL and 0 for NULL <=> 5, never NULL. No IS NOT DISTINCT FROM.
SELECT * FROM tasks WHERE assignee IS :v;
IS and IS NOT are null-safe for any value, not just NULL. IS NOT DISTINCT FROM also works from 3.39.
SELECT * FROM tasks WHERE DECODE(assignee, :v, 1, 0) = 1;
DECODE treats two NULLs as equal, which makes it the compact Oracle idiom. Even 23ai rejects IS NOT DISTINCT FROM.
Whenever either side can be NULL: detecting changed rows during a sync (WHERE t.col IS DISTINCT FROM s.col), comparing audit snapshots, or joining on nullable columns. With plain =, rows with NULL on either side silently drop out.
Yes. SQLite documents IS as equality that treats two NULLs as equal and NULL versus a value as false, which is exactly the standard predicate. 3.39 added the standard spelling as an alias.
It depends on the engine. MySQL's <=> and SQLite's IS can use an index on the column. PostgreSQL does not use a B-tree index for IS NOT DISTINCT FROM, so on large tables rewrite it as (col = $1 OR (col IS NULL AND $1 IS NULL)), or branch in the application on whether the value is NULL. Check the plan on your own engine.
COALESCE everywhere; ISNULL, IFNULL and NVL are the same idea with local names.
CASE is everywhere; IIF, IF and DECODE are the local shortcuts.
NOT IN has a NULL trap that silently empties your result.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.