Challenge - 5 Problems
NULL-safe Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of IS DISTINCT FROM with NULL values
Consider the following SQL query in PostgreSQL:
What is the output of this query?
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;
Attempts:
2 left
💡 Hint
Remember that IS DISTINCT FROM treats NULLs as comparable values.
✗ Incorrect
IS DISTINCT FROM returns false if both values are equal or both are NULL, true otherwise. So 1 IS DISTINCT FROM 1 is false, 1 IS DISTINCT FROM NULL is true, and NULL IS DISTINCT FROM NULL is false.
🧠 Conceptual
intermediate1: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?Attempts:
2 left
💡 Hint
Think about how NULL behaves in normal equality comparisons.
✗ Incorrect
Standard equality operators return unknown (NULL) when comparing NULLs, but IS DISTINCT FROM treats NULLs as equal, so it returns false if both sides are NULL.
📝 Syntax
advanced1: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?Attempts:
2 left
💡 Hint
Check the syntax and meaning of IS DISTINCT FROM.
✗ Incorrect
Option D uses the correct syntax to check if col is different from 10, treating NULLs safely. Option D is invalid syntax. Option D checks for equality, not difference. Option D is syntactically valid but the parentheses are unnecessary and do not change meaning; however, it is valid SQL but option D is the canonical form.
🔧 Debug
advanced2:00remaining
Debugging unexpected results with NULL comparison
A developer writes this query:
They expect to get all users who have a non-NULL last_login. However, the query returns zero rows. What is the reason?
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?
Attempts:
2 left
💡 Hint
Think about how NULL behaves in standard comparisons.
✗ Incorrect
Comparisons with NULL using standard operators return NULL (unknown), which is treated as false in WHERE clauses, so no rows match.
❓ optimization
expert3: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?Attempts:
2 left
💡 Hint
Consider how indexes work with IS DISTINCT FROM and NULLs.
✗ Incorrect
Option A explicitly checks for inequality or NULL, which can use the index efficiently. IS DISTINCT FROM (option A) may not use the index as effectively. Option A fails to include NULLs properly. Option A uses COALESCE which can prevent index usage.