IS DISTINCT FROM for NULL-safe comparison in PostgreSQL - Time & Space Complexity
We want to understand how the time needed to compare values using IS DISTINCT FROM changes as the amount of data grows.
Specifically, how does checking many rows for differences behave as the table gets bigger?
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
WHERE salary IS DISTINCT FROM bonus;
This query selects all employees whose salary is different from their bonus, treating NULL values safely.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing salary and bonus for each row using
IS DISTINCT FROM. - How many times: Once per row in the employees table.
As the number of rows grows, the database must check each row's salary and bonus once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of rows.
Time Complexity: O(n)
This means the time to complete the query grows in a straight line as the table gets bigger.
[X] Wrong: "Using IS DISTINCT FROM makes the query run faster than a normal comparison."
[OK] Correct: The operator only changes how NULLs are handled, not how many comparisons happen. The time still depends on the number of rows.
Understanding how simple comparisons scale helps you explain query performance clearly and shows you know how databases handle NULL values safely.
"What if we added an index on salary and bonus? How would that affect the time complexity of this query?"
Practice
IS DISTINCT FROM operator do in PostgreSQL when comparing two values?Solution
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.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.Final Answer:
It returns true if the values are different or one is NULL and the other is not. -> Option AQuick Check:
IS DISTINCT FROM returns true for different or NULL vs non-NULL [OK]
- Assuming NULL equals NULL
- Confusing IS DISTINCT FROM with = operator
- Thinking it returns true only when both are NULL
a is distinct from column b in PostgreSQL?Solution
Step 1: Recall the correct syntax for NULL-safe comparison
The operatorIS DISTINCT FROMis used asa IS DISTINCT FROM bto compare two values safely including NULLs.Step 2: Evaluate other options
a != buses standard inequality which fails with NULLs.a <> b OR a IS NULL OR b IS NULLis a complex expression but not the standard syntax.a IS NOT DISTINCT FROM bchecks for NOT DISTINCT, which is the opposite.Final Answer:
a IS DISTINCT FROM b -> Option CQuick Check:
UseIS DISTINCT FROMfor NULL-safe inequality [OK]
IS DISTINCT FROM for NULL-safe comparisons [OK]- Using != which fails with NULLs
- Writing complex OR conditions instead of operator
- Confusing IS DISTINCT FROM with IS NOT DISTINCT FROM
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)Solution
Step 1: Understand the condition
This condition returns true ifvalue IS DISTINCT FROM NULLvalueis not NULL, because NULL is treated as a real value.Step 2: Apply condition to each row
Rows withvalue10 and 20 are distinct from NULL, so ids 2 and 4 match.Final Answer:
[2, 4] -> Option BQuick Check:
IS DISTINCT FROM NULL filters non-NULL values [OK]
- Thinking it returns rows with NULL values
- Confusing IS DISTINCT FROM with IS NULL
- Assuming it returns all rows
SELECT * FROM users WHERE email IS DISTINCT FROM NULL;
What is the likely mistake?
Solution
Step 1: Understand what
This condition returns true for all rows whereemail IS DISTINCT FROM NULLmeansemailis NOT NULL.Step 2: Analyze why no rows are returned
If no rows are returned, it means no rows satisfy the condition, so allemailvalues are NULL or the table is empty.Step 3: Eliminate other options
Using IS DISTINCT FROM NULL instead of IS NOT NULL is incorrect becauseIS DISTINCT FROM NULLis 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.Final Answer:
The email column has only NULL values -> Option AQuick Check:
No rows means no non-NULL emails [OK]
- Thinking IS DISTINCT FROM NULL is invalid syntax
- Assuming table is empty without checking
- Confusing IS DISTINCT FROM NULL with IS NULL
records where col1 and col2 differ, treating NULLs as real values. Which query correctly finds these pairs?Solution
Step 1: Understand the goal
We want pairs wherecol1orcol2differ, treating NULLs as real values.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;usesIS DISTINCT FROMwith 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.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 DQuick Check:
Use IS DISTINCT FROM with OR for NULL-safe difference [OK]
- Using <> which fails with NULLs
- Joining on equal ids instead of different ids
- Using IS NOT DISTINCT FROM which finds equal rows
Ready to prove it?
Pass the test and this pattern moves unknown
