Challenge - 5 Problems
NULLIF Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this NULLIF query?
Consider the following query:
What will be the output row?
SELECT NULLIF(5, 5) AS result1, NULLIF(5, 3) AS result2;
What will be the output row?
MySQL
SELECT NULLIF(5, 5) AS result1, NULLIF(5, 3) AS result2;
Attempts:
2 left
💡 Hint
NULLIF returns NULL if the two arguments are equal, otherwise it returns the first argument.
✗ Incorrect
NULLIF(5, 5) returns NULL because both arguments are equal. NULLIF(5, 3) returns 5 because the arguments differ.
🧠 Conceptual
intermediate1:30remaining
What does NULLIF(x, y) do in SQL?
Choose the correct description of the NULLIF function behavior.
Attempts:
2 left
💡 Hint
Think about what happens when the two inputs are the same.
✗ Incorrect
NULLIF compares two expressions and returns NULL if they are equal; otherwise, it returns the first expression.
📝 Syntax
advanced2:30remaining
Which query uses NULLIF correctly to avoid division by zero?
You want to calculate the ratio of column a to column b but avoid division by zero errors. Which query is correct?
MySQL
SELECT a / NULLIF(b, 0) AS ratio FROM table1;
Attempts:
2 left
💡 Hint
NULLIF should replace zero divisor with NULL to prevent error.
✗ Incorrect
NULLIF(b, 0) returns NULL if b is zero, so division by NULL returns NULL instead of error.
❓ optimization
advanced3:00remaining
How does NULLIF improve query performance in conditional expressions?
Given a large table, which use of NULLIF can optimize filtering rows where a column equals a specific value?
Attempts:
2 left
💡 Hint
Think about how NULLIF returns NULL when values match.
✗ Incorrect
NULLIF(column, value) returns NULL if column equals value, so checking IS NULL filters those rows efficiently.
🔧 Debug
expert3:00remaining
Why does this query raise a division by zero error despite using NULLIF?
Query:
Why does it still cause a division by zero error?
SELECT a / NULLIF(0, b) AS ratio FROM table1;
Why does it still cause a division by zero error?
Attempts:
2 left
💡 Hint
Check the order of arguments in NULLIF carefully.
✗ Incorrect
NULLIF(0, b) returns NULL only if 0 equals b, which is false unless b=0. The correct usage is NULLIF(b, 0) to replace zero divisor with NULL.