Using IS DISTINCT FROM for NULL-safe Comparison in PostgreSQL
📖 Scenario: You are managing a customer database where some customers may not have provided their email addresses yet (NULL values). You want to find customers whose email addresses have changed compared to a previous record, but you need to handle NULL values safely without errors.
🎯 Goal: Build a SQL query using IS DISTINCT FROM to safely compare email addresses, including NULL values, to identify customers with changed emails.
📋 What You'll Learn
Create a table called customers with columns id (integer) and email (text).
Insert sample data with some NULL email values.
Create a table called previous_customers with the same structure and sample data.
Write a SQL query using IS DISTINCT FROM to find customers whose current email is different from the previous email, including NULL-safe comparison.
💡 Why This Matters
🌍 Real World
Handling NULL values safely is important in real databases where missing data is common. Using <code>IS DISTINCT FROM</code> helps avoid errors and incorrect results when comparing such data.
💼 Career
Database developers and analysts often need to compare records safely, especially when data can be incomplete. This skill is useful for data cleaning, auditing, and synchronization tasks.
Progress0 / 4 steps
1
Create the customers table and insert data
Create a table called customers with columns id (integer) and email (text). Insert these exact rows: (1, 'alice@example.com'), (2, NULL), (3, 'charlie@example.com').
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the rows exactly as specified.
2
Create the previous_customers table and insert data
Create a table called previous_customers with columns id (integer) and email (text). Insert these exact rows: (1, 'alice@example.com'), (2, 'bob@example.com'), (3, NULL).
PostgreSQL
Hint
Repeat the table creation and insertion steps for previous_customers with the exact rows given.
3
Write a query using IS DISTINCT FROM for NULL-safe comparison
Write a SQL query that selects id and current email from customers joined with previous_customers on id, where customers.email IS DISTINCT FROM previous_customers.email.
PostgreSQL
Hint
Use a JOIN on id and the IS DISTINCT FROM operator in the WHERE clause to compare emails safely.
4
Complete the query with ORDER BY for clear results
Add ORDER BY customers.id at the end of the query to sort the results by id.
PostgreSQL
Hint
Use ORDER BY customers.id to sort the output by customer ID.
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
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 A
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
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.
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.
Final Answer:
a IS DISTINCT FROM b -> Option C
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;
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.
Step 2: Apply condition to each row
Rows with value 10 and 20 are distinct from NULL, so ids 2 and 4 match.
Final Answer:
[2, 4] -> Option B
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
Step 1: Understand what email IS DISTINCT FROM NULL means
This condition returns true for all rows where email is NOT NULL.
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.
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.
Final Answer:
The email column has only NULL values -> Option A
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
Step 1: Understand the goal
We want pairs where col1 or col2 differ, 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; 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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.