Bird
Raised Fist0
PostgreSQLquery~10 mins

IS DISTINCT FROM for NULL-safe comparison in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - IS DISTINCT FROM for NULL-safe comparison
Compare two values
↓
Are both NULL?
Yes→Return FALSE (not distinct)
No↓
Are values equal?
Yes→Return FALSE (not distinct)
No↓
Return TRUE (distinct)
This flow checks if two values are different, treating NULLs as equal only if both are NULL.
Execution Sample
PostgreSQL
SELECT 1 IS DISTINCT FROM 1 AS result1,
       1 IS DISTINCT FROM 2 AS result2,
       NULL IS DISTINCT FROM NULL AS result3,
       NULL IS DISTINCT FROM 1 AS result4;
This query compares pairs of values using IS DISTINCT FROM to safely handle NULLs.
Execution Table
StepComparisonValues ComparedAre Both NULL?Are Values Equal?Result
11 IS DISTINCT FROM 11 and 1NoYesFALSE
21 IS DISTINCT FROM 21 and 2NoNoTRUE
3NULL IS DISTINCT FROM NULLNULL and NULLYesN/AFALSE
4NULL IS DISTINCT FROM 1NULL and 1NoNoTRUE
💡 All comparisons evaluated; NULL-safe logic applied to determine distinctness.
Variable Tracker
VariableStep 1Step 2Step 3Step 4
Values Compared1 and 11 and 2NULL and NULLNULL and 1
Are Both NULL?NoNoYesNo
Are Values Equal?YesNoN/ANo
ResultFALSETRUEFALSETRUE
Key Moments - 2 Insights
Why does NULL IS DISTINCT FROM NULL return FALSE instead of TRUE?
Because IS DISTINCT FROM treats two NULLs as equal, so they are not distinct. See execution_table row 3.
How is IS DISTINCT FROM different from the usual <> operator when NULLs are involved?
The usual <> returns NULL (unknown) if either side is NULL, but IS DISTINCT FROM returns TRUE or FALSE safely. See execution_table rows 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of comparing 1 IS DISTINCT FROM 2?
AFALSE
BNULL
CTRUE
DError
💡 Hint
Check execution_table row 2 under Result column.
At which step does the comparison find both values NULL?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Are Both NULL?' column in execution_table.
If we replaced IS DISTINCT FROM with <> in step 4 (NULL <> 1), what would be the result?
ATRUE
BNULL
CFALSE
DError
💡 Hint
Recall that <> returns NULL if either side is NULL, unlike IS DISTINCT FROM.
Concept Snapshot
IS DISTINCT FROM compares two values treating NULLs safely.
Returns TRUE if values differ or one is NULL and the other is not.
Returns FALSE if values are equal or both NULL.
Unlike <> operator, it never returns NULL.
Useful for NULL-safe equality checks in PostgreSQL.
Full Transcript
The IS DISTINCT FROM operator in PostgreSQL compares two values and returns TRUE if they are different, including when one is NULL and the other is not. It returns FALSE if the values are equal or both are NULL. This operator is NULL-safe, meaning it treats two NULLs as equal, unlike the standard <> operator which returns NULL when comparing with NULLs. The execution flow first checks if both values are NULL, then if they are equal, and finally returns TRUE if neither condition is met. This makes IS DISTINCT FROM very useful for comparisons where NULLs might appear and you want a clear TRUE or FALSE result.

Practice

(1/5)
1. What does the IS DISTINCT FROM operator do in PostgreSQL when comparing two values?
easy
A. It returns true if the values are different or one is NULL and the other is not.
B. It returns true only if both values are NULL.
C. It returns false if the values are different.
D. It treats NULL as equal to any value.

Solution

  1. Step 1: Understand the behavior of IS DISTINCT FROM

    This operator treats NULL as a real value and compares two values safely even if one or both are NULL.
  2. Step 2: Analyze the comparison results

    It returns true if the values differ or if one is NULL and the other is not, otherwise false.
  3. Final Answer:

    It returns true if the values are different or one is NULL and the other is not. -> Option A
  4. Quick Check:

    IS DISTINCT FROM returns true for different or NULL vs non-NULL [OK]
Hint: Think: NULL counts as a real value in IS DISTINCT FROM [OK]
Common Mistakes:
  • Assuming NULL equals NULL
  • Confusing IS DISTINCT FROM with = operator
  • Thinking it returns true only when both are NULL
2. Which of the following is the correct syntax to check if column a is distinct from column b in PostgreSQL?
easy
A. a <> b OR a IS NULL OR b IS NULL
B. a != b
C. a IS DISTINCT FROM b
D. a IS NOT DISTINCT FROM b

Solution

  1. Step 1: Recall the correct syntax for NULL-safe comparison

    The operator IS DISTINCT FROM is used as a IS DISTINCT FROM b to compare two values safely including NULLs.
  2. Step 2: Evaluate other options

    a != b uses standard inequality which fails with NULLs. a <> b OR a IS NULL OR b IS NULL is a complex expression but not the standard syntax. a IS NOT DISTINCT FROM b checks for NOT DISTINCT, which is the opposite.
  3. Final Answer:

    a IS DISTINCT FROM b -> Option C
  4. Quick Check:

    Use IS DISTINCT FROM for NULL-safe inequality [OK]
Hint: Use IS DISTINCT FROM for NULL-safe comparisons [OK]
Common Mistakes:
  • Using != which fails with NULLs
  • Writing complex OR conditions instead of operator
  • Confusing IS DISTINCT FROM with IS NOT DISTINCT FROM
3. Given the table items with columns id and value, what will this query return?
SELECT id FROM items WHERE value IS DISTINCT FROM NULL;

Assuming items has rows:
(1, NULL), (2, 10), (3, NULL), (4, 20)
medium
A. [1, 2, 3, 4]
B. [2, 4]
C. [1, 3]
D. [] (empty list)

Solution

  1. Step 1: Understand the condition value IS DISTINCT FROM NULL

    This condition returns true if value is not NULL, because NULL is treated as a real value.
  2. Step 2: Apply condition to each row

    Rows with value 10 and 20 are distinct from NULL, so ids 2 and 4 match.
  3. Final Answer:

    [2, 4] -> Option B
  4. Quick Check:

    IS DISTINCT FROM NULL filters non-NULL values [OK]
Hint: IS DISTINCT FROM NULL means NOT NULL [OK]
Common Mistakes:
  • Thinking it returns rows with NULL values
  • Confusing IS DISTINCT FROM with IS NULL
  • Assuming it returns all rows
4. You wrote this query but it returns no rows:
SELECT * FROM users WHERE email IS DISTINCT FROM NULL;

What is the likely mistake?
medium
A. The email column has only NULL values
B. Using IS DISTINCT FROM NULL instead of IS NOT NULL
C. IS DISTINCT FROM cannot be used with NULL
D. The table users is empty

Solution

  1. Step 1: Understand what email IS DISTINCT FROM NULL means

    This condition returns true for all rows where email is NOT NULL.
  2. Step 2: Analyze why no rows are returned

    If no rows are returned, it means no rows satisfy the condition, so all email values are NULL or the table is empty.
  3. Step 3: Eliminate other options

    Using IS DISTINCT FROM NULL instead of IS NOT NULL is incorrect because IS DISTINCT FROM NULL is valid. The table users is empty is possible but less likely. IS DISTINCT FROM cannot be used with NULL is false because the operator supports NULL.
  4. Final Answer:

    The email column has only NULL values -> Option A
  5. Quick Check:

    No rows means no non-NULL emails [OK]
Hint: No rows means condition never true; check NULL presence [OK]
Common Mistakes:
  • Thinking IS DISTINCT FROM NULL is invalid syntax
  • Assuming table is empty without checking
  • Confusing IS DISTINCT FROM NULL with IS NULL
5. You want to find all pairs of rows in table records where col1 and col2 differ, treating NULLs as real values. Which query correctly finds these pairs?
hard
A. SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 IS NOT DISTINCT FROM r2.col1 AND r1.col2 IS NOT DISTINCT FROM r2.col2;
B. SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 <> r2.col1 OR r1.col2 <> r2.col2;
C. SELECT * FROM records r1 JOIN records r2 ON r1.id = r2.id WHERE r1.col1 IS DISTINCT FROM r2.col1 AND r1.col2 IS DISTINCT FROM r2.col2;
D. SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 IS DISTINCT FROM r2.col1 OR r1.col2 IS DISTINCT FROM r2.col2;

Solution

  1. Step 1: Understand the goal

    We want pairs where col1 or col2 differ, treating NULLs as real values.
  2. Step 2: Analyze each option

    SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 IS DISTINCT FROM r2.col1 OR r1.col2 IS DISTINCT FROM r2.col2; uses IS DISTINCT FROM with OR, correctly finding pairs differing in either column. SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 <> r2.col1 OR r1.col2 <> r2.col2; uses <> which fails with NULLs. SELECT * FROM records r1 JOIN records r2 ON r1.id = r2.id WHERE r1.col1 IS DISTINCT FROM r2.col1 AND r1.col2 IS DISTINCT FROM r2.col2; joins on equal ids, so no pairs. SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 IS NOT DISTINCT FROM r2.col1 AND r1.col2 IS NOT DISTINCT FROM r2.col2; finds pairs where columns are NOT distinct, opposite of goal.
  3. Final Answer:

    SELECT * FROM records r1 JOIN records r2 ON r1.id <> r2.id WHERE r1.col1 IS DISTINCT FROM r2.col1 OR r1.col2 IS DISTINCT FROM r2.col2 -> Option D
  4. Quick Check:

    Use IS DISTINCT FROM with OR for NULL-safe difference [OK]
Hint: Use IS DISTINCT FROM with OR to find any column differences [OK]
Common Mistakes:
  • Using <> which fails with NULLs
  • Joining on equal ids instead of different ids
  • Using IS NOT DISTINCT FROM which finds equal rows
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard