SQL IS DISTINCT FROM operator
Equality that treats two NULLs as equal instead of unknown.
- 類別
- 條件
- Returns
boolean- In this category
- 27 of 43
語法
a IS DISTINCT FROM b | a IS NOT DISTINCT FROM b
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | 是 | SQL Server 2022+ |
| PostgreSQL | 是 | Supported under this name. |
| MySQL | Not built in | MySQL has no IS DISTINCT FROM; use <=> |
| SQLite | 是 | 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 |
範例
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;
小知識
- 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.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, SQLite, Oracle.
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.