Function or operator · Filters and conditions

IS DISTINCT FROM: comparing values including NULL

IS DISTINCT FROM compares two values including NULL and always returns true or false. Two NULLs count as equal for this predicate.

Before continuing: This entry is part of the NULL guide. If you are new to the topic, start with IS NULL / IS NOT NULL.

Example

The query below builds four pairs and compares each one both ways. Verified in SQLite 3.46.1.

True or false, even with NULL
WITH comparisons(current, expected) AS (
  VALUES
    ('shipped',  'shipped'),
    ('pending','shipped'),
    (NULL,       'shipped'),
    (NULL,       NULL)
)
SELECT
  current,
  expected,
  current IS DISTINCT FROM expected AS is_distinct,
  current IS NOT DISTINCT FROM expected AS is_equal_including_null
FROM comparisons;

How it is used

With ordinary operators, NULL = NULL and NULL <> NULL are neither true nor false: they give an unknown result. In a WHERE, that unknown result does not get through the filter.

IS NOT DISTINCT FROM answers a more practical question: “should these two values count as equal, including when both are NULL?”. IS DISTINCT FROM answers the opposite.

Avoid this confusion

Confusing “different” with IS NOT NULL

a IS DISTINCT FROM b compares two values. a IS NOT NULL only asks whether a has a known value.

Using COALESCE with a sentinel value

COALESCE(a, 'N/A') = COALESCE(b, 'N/A') can mix up a real NULL with a valid value that happens to be 'N/A'. The specific predicate avoids that ambiguity.

Key differences between engines

PostgreSQL documents IS DISTINCT FROM and IS NOT DISTINCT FROM as comparison predicates that treat NULL as a comparable value. SQLite supports the same forms and also documents IS/IS NOT as its own shorthand.

SQL Server has supported IS [NOT] DISTINCT FROM since SQL Server 2022. MySQL uses another operator for NULL-safe equality: <=>. So do not copy this syntax between engines without checking the version.

Detecting changes without losing unknown states

When synchronising data, you need to know whether the stored value and the incoming value differ, even when one of them is missing. An ordinary inequality can leave that case unknown. A null-aware comparison lets you treat “known vs unknown” as a change and “unknown vs unknown” as no change, under that specific policy.

Do not confuse that equivalence with the two values being genuinely equal in the real world. Two unknown dates do not prove both events happened on the same day; they are just treated as the same state for this comparison. Define the update policy before applying it to every column, especially if a missing input means “leave unchanged” rather than “clear the value”.

The example table covers all four edge cases: equal known values, different known values, known vs null, and two nulls. Do not replace nulls with an arbitrary sentinel that could also be valid data; zero, for instance, can be a legitimate price. The available forms depend on the engine, so a portable expression must check both its syntax and these four results.

Self-contained case to check in SQLite

SQLite lets you check all four cases directly without inventing a value to stand in for NULL.

Data and query · SQLite
WITH changes(id, before, after) AS (
  VALUES (1, 10, 10), (2, 10, 20), (3, 10, NULL), (4, NULL, NULL)
)
SELECT id, before, after, before IS DISTINCT FROM after AS changed
FROM changes ORDER BY id;
Verified result of the self-contained case
idbeforeafterchanged
110100
210201
310NULL1
4NULLNULL0

The flags 1 and 0 represent the truth of the comparison in SQLite. Two nulls count as not distinct for this operator.

Check your interpretation: Which change would an ordinary filter before <> after miss?

Show answer

The one in row 3, from 10 to NULL. before <> after is unknown when one side is NULL, and WHERE would discard the row even though the value did change.

Technical questions: PostgreSQL: comparisons and NULL handling (opens in a new tab).