Sub topic 4
Testing for NULL with IS NULL and IS DISTINCT FROM
This chapter assumes you know basic SELECT, WHERE, and ORDER BY, and that you have seen three-valued logic (TRUE, FALSE, UNKNOWN) and why = NULL never matches. Here you learn the operators SQL provides to test for NULL directly, and the NULL-safe comparison IS DISTINCT FROM. All examples use the employees table below. Ada has no manager, and Ada and Cy have no commission recorded.
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(50) NOT NULL,
manager_id INTEGER,
commission NUMERIC(8,2)
);
INSERT INTO employees (id, name, manager_id, commission) VALUES
(1, 'Ada', NULL, NULL),
(2, 'Ben', 1, 100.00),
(3, 'Cy', 1, NULL),
(4, 'Di', 2, 300.00);Why a special operator is needed
An ordinary comparison such as commission = NULL yields UNKNOWN for every row, even a row whose commission is NULL. WHERE keeps only rows where the condition is TRUE, so nothing is returned. The same happens with <>, <, >, and = between two columns that are both NULL.
SQL therefore defines predicates that ask a different question. They do not compare two values. They ask about the state of one value, and they always return TRUE or FALSE, never UNKNOWN.
IS NULL and IS NOT NULL
IS NULL returns TRUE when the value is NULL and FALSE otherwise. IS NOT NULL is the opposite.
-- Returns no rows: commission = NULL evaluates to UNKNOWN for every row.
SELECT name FROM employees WHERE commission = NULL;
-- Returns Ada and Cy.
SELECT name FROM employees WHERE commission IS NULL;
-- Returns only Di: rows where the comparison is UNKNOWN are filtered out.
SELECT name FROM employees WHERE commission <> 100;The first query returns nothing and raises no error. This is the dangerous part: the query is valid, and the empty result looks like a legitimate answer.
The third query shows a related trap. Ada and Cy have no commission, and a person might expect them to count as "not equal to 100". The database cannot say whether an unknown value differs from 100, so the comparison is UNKNOWN and those rows are dropped. Only Di (300.00) remains. Ben is excluded because his commission equals 100.
Writing negations correctly
Place NOT carefully. These forms are equivalent in standard SQL:
commission IS NOT NULLNOT (commission IS NULL)
Avoid NOT commission IS NULL in complex conditions because operator precedence makes it harder to read. Prefer IS NOT NULL.
Do not confuse these with commission != NULL or commission <> NULL. Both yield UNKNOWN for every row, just as = NULL does.
Applying IS NULL to more than columns
IS NULL works on any expression, not only column names.
SELECT name
FROM employees
WHERE commission * 12 IS NULL;Because arithmetic on NULL gives NULL, this returns Ada and Cy. It is equivalent to testing the column itself here, but it also catches NULLs produced by other parts of the expression.
Combining NULL tests with other conditions
A common requirement is "commission is below 200, or missing". Write the NULL test explicitly with OR.
SELECT name
FROM employees
WHERE commission < 200 OR commission IS NULL;This returns Ada, Ben, and Cy. For Ada, commission < 200 is UNKNOWN and commission IS NULL is TRUE. UNKNOWN OR TRUE is TRUE, so the row is kept. For Di, 300 < 200 is FALSE and IS NULL is FALSE, so the row is dropped.
IS DISTINCT FROM
The IS NULL test handles one value. Often you need to compare two values, either of which might be NULL. For that, standard SQL provides IS DISTINCT FROM and IS NOT DISTINCT FROM.
IS DISTINCT FROM behaves like <>, except that it treats NULL as an ordinary, comparable value:
- Two NULLs are not distinct from each other.
- A NULL and a non-NULL value are distinct.
- Two non-NULL values are distinct when they differ, as with
<>.
The result is always TRUE or FALSE.
-- Standard SQL (for example PostgreSQL): NULL-safe inequality.
-- Returns Ada, Cy, and Di.
SELECT name FROM employees WHERE commission IS DISTINCT FROM 100;Compare this with the <> query earlier, which returned only Di. With IS DISTINCT FROM, Ada and Cy are included because NULL is distinct from 100. Ben is excluded because 100 is not distinct from 100.
Truth table
The table shows the result for each combination of operands.
| a | b | a = b | a IS NOT DISTINCT FROM b | a <> b | a IS DISTINCT FROM b |
|---|---|---|---|---|---|
| 1 | 1 | TRUE | TRUE | FALSE | FALSE |
| 1 | 2 | FALSE | FALSE | TRUE | TRUE |
| 1 | NULL | UNKNOWN | FALSE | UNKNOWN | TRUE |
| NULL | 1 | UNKNOWN | FALSE | UNKNOWN | TRUE |
| NULL | NULL | UNKNOWN | TRUE | UNKNOWN | FALSE |
The last row is the important one. NULL = NULL is UNKNOWN, but NULL IS NOT DISTINCT FROM NULL is TRUE.
Comparing two columns
IS NOT DISTINCT FROM is useful when you want to find rows where two nullable columns "match", counting a pair of NULLs as a match.
-- Finds rows where a and b are equal, or both NULL.
SELECT *
FROM pairs
WHERE a IS NOT DISTINCT FROM b;Without it, you would write the longer and error-prone form:
SELECT *
FROM pairs
WHERE a = b OR (a IS NULL AND b IS NULL);Both forms give the same answer, but the first is shorter and cannot lose the NULL case if someone edits one half of the expression.
Comparing against a parameter
Search forms often pass a value that might be NULL. A query written as WHERE manager_id = :mgr never matches when :mgr is NULL. Use the NULL-safe form instead:
SELECT name
FROM employees
WHERE manager_id IS NOT DISTINCT FROM :mgr;With :mgr set to 1, this returns Ben and Cy. With :mgr set to NULL, it returns Ada. The placeholder syntax (:mgr) varies by driver.
Support differs between systems
IS DISTINCT FROM is part of the SQL standard, but not every database implements it with the same syntax. The IS NULL and IS NOT NULL predicates work everywhere.
| System | NULL-safe comparison |
|---|---|
| PostgreSQL | `IS [NOT] DISTINCT FROM` |
| SQLite | `IS` and `IS NOT` work as NULL-safe equality and inequality; recent versions also accept `IS [NOT] DISTINCT FROM` |
| MySQL | `<=>` is NULL-safe equality; `IS [NOT] DISTINCT FROM` is not available in older versions |
| SQL Server | `IS [NOT] DISTINCT FROM` in SQL Server 2022 and later; older versions need `OR ... IS NULL` expressions |
| Oracle | No operator in older versions; use `DECODE` or explicit `OR` tests |
Check your database's documentation before relying on the syntax. If you need portable code, the explicit form a = b OR (a IS NULL AND b IS NULL) works on every system.
Performance considerations
IS NULL and IS NOT NULL can usually use an index, though whether NULLs are stored in the index depends on the system. Oracle's B-tree indexes, for example, do not store rows where every indexed column is NULL, so WHERE col IS NULL may not use a single-column index there. PostgreSQL and most other systems do index NULLs.
IS DISTINCT FROM is often not indexable in the same way as a plain =. Some optimizers rewrite it to an equality plus a NULL test, and others evaluate it row by row. If a query is slow, compare the execution plan with an explicit OR ... IS NULL form.
Common mistakes
- Writing
= NULLor<> NULL. The query is valid but returns nothing. - Using
<>and expecting NULL rows to appear. They are filtered out as UNKNOWN. AddOR col IS NULL, or useIS DISTINCT FROM. - Using
IS DISTINCT FROMon a system that does not support it. The error is a syntax error, so it is caught quickly. - Putting a NULL test in the wrong place with
ANDandOR. Add parentheses so the grouping is explicit.
Summary of the rules
- Use
IS NULLandIS NOT NULLto test a single value. They never return UNKNOWN. - Use
IS DISTINCT FROMandIS NOT DISTINCT FROMto compare two values when NULL should behave like an ordinary value. - Use
=and<>when NULL on either side should mean "no answer", and the row should be filtered out. - Check your database's syntax before using the NULL-safe operators.
Exercise
Using the employees table, which names does each query return? (a) WHERE commission <> 300, (b) WHERE commission IS DISTINCT FROM 300, (c) WHERE commission IS NOT DISTINCT FROM NULL.
Show answer
(a) Only Ben. Ada and Cy have a NULL commission, so the comparison is UNKNOWN and they are dropped, and Di's commission equals 300. (b) Ada, Ben, and Cy. NULL is distinct from 300, Ben's 100 differs from 300, and Di's 300 does not. (c) Ada and Cy. Their NULL commission is not distinct from NULL, so the condition is TRUE for them. Ben and Di have non-NULL values, which are distinct from NULL.
Exercise
Rewrite WHERE managerid <> 1 so that Ada (whose managerid is NULL) is included, first with IS DISTINCT FROM and then with a portable form that does not use it.
Show answer
With the standard operator: WHERE managerid IS DISTINCT FROM 1. Portable form: WHERE managerid <> 1 OR managerid IS NULL. Both return Ada and Di, because Ada's managerid is NULL and Di's is 2, while Ben and Cy have manager_id 1.