0
0
PostgreSQLquery~20 mins

IS DISTINCT FROM for NULL-safe comparison in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULL-safe Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of IS DISTINCT FROM with NULL values
Consider the following SQL query in PostgreSQL:

SELECT 1 IS DISTINCT FROM 1 AS result1,
1 IS DISTINCT FROM NULL AS result2,
NULL IS DISTINCT FROM NULL AS result3;

What is the output of this query?
PostgreSQL
SELECT 1 IS DISTINCT FROM 1 AS result1,
       1 IS DISTINCT FROM NULL AS result2,
       NULL IS DISTINCT FROM NULL AS result3;
Aresult1: false, result2: true, result3: false
Bresult1: true, result2: true, result3: true
Cresult1: false, result2: false, result3: true
Dresult1: true, result2: false, result3: false
Attempts:
2 left
💡 Hint
Remember that IS DISTINCT FROM treats NULLs as comparable values.
🧠 Conceptual
intermediate
1:30remaining
Purpose of IS DISTINCT FROM in SQL
What is the main advantage of using IS DISTINCT FROM over the standard = or <> operators when comparing columns that may contain NULL values?
AIt converts NULL values to zero before comparison.
BIt ignores NULL values and only compares non-NULL values.
CIt treats NULL values as equal, avoiding unknown results in comparisons.
DIt raises an error if NULL values are present.
Attempts:
2 left
💡 Hint
Think about how NULL behaves in normal equality comparisons.
📝 Syntax
advanced
1:30remaining
Identify the valid use of IS DISTINCT FROM
Which of the following SQL WHERE clauses correctly uses IS DISTINCT FROM to filter rows where column col is different from 10, including NULL-safe comparison?
AWHERE col != 10 IS DISTINCT FROM TRUE
BWHERE col IS DISTINCT FROM (10)
CWHERE col IS NOT DISTINCT FROM 10
DWHERE col IS DISTINCT FROM 10
Attempts:
2 left
💡 Hint
Check the syntax and meaning of IS DISTINCT FROM.
🔧 Debug
advanced
2:00remaining
Debugging unexpected results with NULL comparison
A developer writes this query:

SELECT * FROM users WHERE last_login <> NULL;

They expect to get all users who have a non-NULL last_login. However, the query returns zero rows. What is the reason?
AThe query syntax is invalid and causes an error.
BThe expression 'last_login <> NULL' always evaluates to NULL, so no rows match.
CThe database treats NULL as zero, so no rows match.
DThe query returns all rows because NULL is ignored.
Attempts:
2 left
💡 Hint
Think about how NULL behaves in standard comparisons.
optimization
expert
3:00remaining
Optimizing NULL-safe comparisons in large datasets
You have a large table with millions of rows and a nullable column status. You want to find rows where status is different from 'active', including NULL-safe comparison. Which query is likely to perform best in PostgreSQL, assuming an index on status exists?
ASELECT * FROM table WHERE status <> 'active' OR status IS NULL;
BSELECT * FROM table WHERE NOT (status = 'active');
CSELECT * FROM table WHERE COALESCE(status, '') <> 'active';
DSELECT * FROM table WHERE status IS DISTINCT FROM 'active';
Attempts:
2 left
💡 Hint
Consider how indexes work with IS DISTINCT FROM and NULLs.