// conditional reference

SQL IS DISTINCT FROM operator

Equality that treats two NULLs as equal instead of unknown.

Catégorie
Conditionnel
Returns
boolean
In this category
27 of 43

Syntaxe

a IS DISTINCT FROM b | a IS NOT DISTINCT FROM b

Prise en charge des moteurs

MoteurSupportNotes
SQL Server Oui SQL Server 2022+
PostgreSQL Oui Supported under this name.
MySQL Not built in MySQL has no IS DISTINCT FROM; use <=>
SQLite Oui IS DISTINCT FROM since SQLite 3.39; IS / IS NOT always worked
Oracle Different name or syntax not in 23ai; use DECODE(a, b, 1, 0) = 1 or SYS_OP_MAP_NONNULL

Exemples

PostgreSQL

SELECT * FROM t WHERE a IS DISTINCT FROM b;

SQL Server

SELECT * FROM t WHERE a IS NOT DISTINCT FROM b;

SQLite

SELECT * FROM t WHERE a IS NOT b;

Bon à savoir

  • Two NULLs are NOT DISTINCT (equal), so a IS NOT DISTINCT FROM b is the NULL-safe =.
  • IS DISTINCT FROM never returns UNKNOWN: it is two-valued, which makes NOT of it behave intuitively.
  • Oracle's DECODE treats two NULLs as equal, unlike CASE, which makes DECODE(a, b, 1, 0) = 1 the local recipe.
  • SQLite's IS and IS NOT have been the null-safe comparison for years (any operands since 3.6.19); the IS DISTINCT FROM spelling arrived in 3.39.

Questions about IS DISTINCT FROM

Is NULL IS NOT DISTINCT FROM NULL true?

Yes. That is the feature: null-safe equality where two unknowns count as the same.

What is MySQL's equivalent?

<=>: NULL <=> NULL is 1, unlike NULL = NULL.

How about Oracle?

DECODE(a, b, 1, 0) = 1, or the undocumented SYS_OP_MAP_NONNULL on both sides.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.