0
0
MySQLquery~20 mins

NULLIF function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULLIF Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this NULLIF query?
Consider the following query:
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;
A{"result1": 5, "result2": null}
B{"result1": 0, "result2": 5}
C{"result1": null, "result2": null}
D{"result1": null, "result2": 5}
Attempts:
2 left
💡 Hint
NULLIF returns NULL if the two arguments are equal, otherwise it returns the first argument.
🧠 Conceptual
intermediate
1:30remaining
What does NULLIF(x, y) do in SQL?
Choose the correct description of the NULLIF function behavior.
AAlways returns NULL regardless of inputs.
BReturns y if x equals y; otherwise returns NULL.
CReturns NULL if x equals y; otherwise returns x.
DReturns x if x equals y; otherwise returns y.
Attempts:
2 left
💡 Hint
Think about what happens when the two inputs are the same.
📝 Syntax
advanced
2: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;
ASELECT a / b NULLIF(0) AS ratio FROM table1;
BSELECT a / NULLIF(b, 0) AS ratio FROM table1;
CSELECT NULLIF(a, 0) / b AS ratio FROM table1;
DSELECT a / NULLIF(0, b) AS ratio FROM table1;
Attempts:
2 left
💡 Hint
NULLIF should replace zero divisor with NULL to prevent error.
optimization
advanced
3: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?
AUsing NULLIF(column, value) IS NULL to filter rows equal to value.
BUsing column = NULLIF(column, value) to filter rows equal to value.
CUsing NULLIF(column, value) = value to filter rows equal to value.
DUsing NULLIF(column, value) != NULL to filter rows equal to value.
Attempts:
2 left
💡 Hint
Think about how NULLIF returns NULL when values match.
🔧 Debug
expert
3:00remaining
Why does this query raise a division by zero error despite using NULLIF?
Query:
SELECT a / NULLIF(0, b) AS ratio FROM table1;

Why does it still cause a division by zero error?
ABecause NULLIF arguments are reversed; it should be NULLIF(b, 0).
BBecause NULLIF(0, b) returns NULL when b is zero, which causes division by zero.
CBecause NULLIF(0, b) returns 0 when b is not zero, causing division by zero.
DBecause division by NULL is not allowed in MySQL.
Attempts:
2 left
💡 Hint
Check the order of arguments in NULLIF carefully.