0
0
SQLquery~20 mins

NULL behavior in comparisons in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of NULL comparison in WHERE clause
Consider a table Employees with a column ManagerID that can be NULL. What rows will this query return?
SELECT * FROM Employees WHERE ManagerID = NULL;
SQL
SELECT * FROM Employees WHERE ManagerID = NULL;
AAll rows are returned regardless of ManagerID value.
BAll rows where ManagerID is NULL are returned.
CNo rows are returned because NULL = NULL is unknown.
DThe query causes a syntax error.
Attempts:
2 left
💡 Hint
Remember that NULL means unknown, so comparisons with NULL don't behave like normal values.
query_result
intermediate
2:00remaining
Effect of IS NULL vs = NULL in filtering
Given a table Orders with a column ShippedDate that can be NULL, which query returns all orders that have not been shipped?
1) SELECT * FROM Orders WHERE ShippedDate = NULL;
2) SELECT * FROM Orders WHERE ShippedDate IS NULL;
SQL
SELECT * FROM Orders WHERE ShippedDate IS NULL;
ANeither query returns any rows.
BOnly query 1 returns the correct rows.
CBoth queries return the same rows.
DOnly query 2 returns the correct rows.
Attempts:
2 left
💡 Hint
Think about how SQL treats NULL in equality comparisons versus IS NULL.
🧠 Conceptual
advanced
2:00remaining
Understanding three-valued logic with NULL
In SQL, what is the result of the expression (NULL <> 5) in a WHERE clause filter?
ATRUE
BUNKNOWN (treated as FALSE in WHERE)
CFALSE
DSyntax error
Attempts:
2 left
💡 Hint
Remember SQL uses three-valued logic: TRUE, FALSE, and UNKNOWN.
📝 Syntax
advanced
2:00remaining
Correct syntax to check for NULL in SQL
Which of the following SQL WHERE clauses correctly filters rows where the column Price is NULL?
AWHERE Price IS NULL
BWHERE Price == NULL
CWHERE Price = NULL
DWHERE Price != NULL
Attempts:
2 left
💡 Hint
Think about the special keyword used to check NULL values.
optimization
expert
3:00remaining
Optimizing queries with NULL checks
You want to find all customers who have not placed any orders. The Orders table has a nullable CustomerID column. Which query is most efficient and correct?
A) SELECT * FROM Customers WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);
B) SELECT * FROM Customers WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders WHERE CustomerID IS NOT NULL);
C) SELECT * FROM Customers WHERE CustomerID NOT EXISTS (SELECT 1 FROM Orders WHERE Orders.CustomerID = Customers.CustomerID);
D) SELECT * FROM Customers WHERE CustomerID IS NULL;
AOption C
BOption A
COption D
DOption B
Attempts:
2 left
💡 Hint
Consider how NULLs affect NOT IN and EXISTS clauses.