// dialect comparison

NULL-safe equality: IS NOT DISTINCT FROM, <=> and IS

NULL = NULL is unknown, not true, unless you use a null-safe operator.

Filed under Querying

The verdict

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.

Where they differ

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.

How each engine writes it

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)).

PostgreSQL

SELECT * FROM tasks WHERE assignee IS NOT DISTINCT FROM $1;

The negation is IS DISTINCT FROM. $1 is a bind parameter.

MySQL

SELECT * FROM tasks WHERE assignee <=> @v;

<=> returns 1 for NULL <=> NULL and 0 for NULL <=> 5, never NULL. No IS NOT DISTINCT FROM.

SQLite

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.

Oracle

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.

Questions that come up

When does this matter?

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.

Is SQLite's IS the same as IS NOT DISTINCT FROM?

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.

Can the null-safe form still use an index?

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.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.