Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does NULL represent in a database?
easy
A. False boolean
B. Zero value
C. Empty string
D. Missing or unknown data

Solution

  1. Step 1: Understand the meaning of NULL

    NULL in databases means the data is missing or unknown, not zero or empty.
  2. Step 2: Differentiate NULL from other values

    Zero, empty string, and false are actual values, while NULL means no value is stored.
  3. Final Answer:

    Missing or unknown data -> Option D
  4. Quick Check:

    NULL means missing or unknown data [OK]
Hint: NULL means unknown or missing, not zero or empty [OK]
Common Mistakes:
  • Confusing NULL with zero
  • Thinking NULL is an empty string
  • Assuming NULL means false
2. Which of the following is the correct way to check if a column col is NULL in SQL?
easy
A. WHERE col = NULL
B. WHERE col == NULL
C. WHERE col IS NULL
D. WHERE col <> NULL

Solution

  1. Step 1: Recall SQL syntax for NULL checks

    In SQL, to check for NULL, you must use IS NULL because = NULL does not work.
  2. Step 2: Eliminate incorrect syntax

    col = NULL and col == NULL are invalid for NULL checks; col <> NULL also does not work.
  3. Final Answer:

    WHERE col IS NULL -> Option C
  4. Quick Check:

    Use IS NULL to check NULL values [OK]
Hint: Use IS NULL, not = or ==, to check for NULL [OK]
Common Mistakes:
  • Using = NULL instead of IS NULL
  • Using == NULL which is invalid in SQL
  • Using <> NULL to check for NULL
3. Given a table users with a column age that contains some NULL values, what will this query return?
SELECT * FROM users WHERE age <> 30;
medium
A. All rows where age is not 30, including rows with NULL age
B. Only rows where age is not 30 and not NULL
C. Only rows where age is 30
D. No rows, because NULL causes error

Solution

  1. Step 1: Understand comparison with NULL

    Comparisons like age <> 30 return UNKNOWN for NULL values, so those rows are excluded.
  2. Step 2: Determine which rows are returned

    Only rows where age is not 30 and age is not NULL will be returned.
  3. Final Answer:

    Only rows where age is not 30 and not NULL -> Option B
  4. Quick Check:

    Comparisons with NULL exclude NULL rows [OK]
Hint: Comparisons exclude NULL rows, so NULLs are ignored [OK]
Common Mistakes:
  • Assuming NULL rows are included
  • Thinking NULL causes errors in comparisons
  • Confusing <> with IS NOT NULL
4. You wrote this query to find rows where status is not NULL:
SELECT * FROM orders WHERE status <> NULL;

Why does it return no rows, and how to fix it?
medium
A. Comparison with NULL returns UNKNOWN; use status IS NOT NULL
B. Syntax error; use status != NULL
C. NULL is treated as zero; use status <> 0
D. Query is correct; no rows have status

Solution

  1. Step 1: Understand NULL comparison behavior

    Comparing with NULL using <> returns UNKNOWN, so no rows match.
  2. Step 2: Use correct syntax to check NOT NULL

    Use status IS NOT NULL to find rows where status is not NULL.
  3. Final Answer:

    Comparison with NULL returns UNKNOWN; use status IS NOT NULL -> Option A
  4. Quick Check:

    Use IS NOT NULL to check for non-NULL values [OK]
Hint: Use IS NOT NULL, not <> NULL, to find non-NULL rows [OK]
Common Mistakes:
  • Using <> NULL instead of IS NOT NULL
  • Assuming <> NULL works like != NULL
  • Thinking NULL is zero or empty
5. You want to select all rows from products where discount is either NULL or greater than 10. Which query correctly returns these rows?
hard
A. SELECT * FROM products WHERE discount > 10 OR discount IS NULL;
B. SELECT * FROM products WHERE discount > 10 AND discount IS NULL;
C. SELECT * FROM products WHERE discount > 10 OR discount = NULL;
D. SELECT * FROM products WHERE discount > 10 AND discount = NULL;

Solution

  1. Step 1: Understand NULL checks with OR condition

    To include NULL values, use IS NULL, not = NULL.
  2. Step 2: Combine conditions correctly

    Use OR to select rows where discount is greater than 10 or discount is NULL.
  3. Final Answer:

    SELECT * FROM products WHERE discount > 10 OR discount IS NULL; -> Option A
  4. Quick Check:

    Use IS NULL with OR to include NULLs [OK]
Hint: Use OR with IS NULL to include NULL values in conditions [OK]
Common Mistakes:
  • Using = NULL instead of IS NULL
  • Using AND instead of OR with NULL checks
  • Assuming NULL compares like normal values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard