0
0
SQLquery~20 mins

COALESCE and NULLIF as CASE shortcuts in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
COALESCE and NULLIF Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of COALESCE with NULL values
Given the table Employees with columns id, name, and nickname, what is the output of this query?
SELECT id, COALESCE(nickname, name) AS display_name FROM Employees ORDER BY id;

Assume the data:
id | name    | nickname
1  | Alice   | NULL
2  | Bob     | Bobby
3  | Charlie | NULL
SQL
SELECT id, COALESCE(nickname, name) AS display_name FROM Employees ORDER BY id;
A[{"id":1,"display_name":null},{"id":2,"display_name":"Bobby"},{"id":3,"display_name":null}]
B[{"id":1,"display_name":"Alice"},{"id":2,"display_name":"Bobby"},{"id":3,"display_name":"Charlie"}]
C[{"id":1,"display_name":"nickname"},{"id":2,"display_name":"Bobby"},{"id":3,"display_name":"nickname"}]
D[{"id":1,"display_name":"Alice"},{"id":2,"display_name":"Bob"},{"id":3,"display_name":"Charlie"}]
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value from its arguments.
query_result
intermediate
2:00remaining
Using NULLIF to avoid division by zero
Consider a table Sales with columns region, total_sales, and total_customers. What is the output of this query?
SELECT region, total_sales / NULLIF(total_customers, 0) AS avg_sale_per_customer FROM Sales ORDER BY region;

Given data:
region | total_sales | total_customers
East   | 1000        | 10
West   | 500         | 0
North  | 750         | 15
SQL
SELECT region, total_sales / NULLIF(total_customers, 0) AS avg_sale_per_customer FROM Sales ORDER BY region;
A[{"region":"East","avg_sale_per_customer":100},{"region":"North","avg_sale_per_customer":50},{"region":"West","avg_sale_per_customer":null}]
B[{"region":"East","avg_sale_per_customer":100},{"region":"North","avg_sale_per_customer":50},{"region":"West","avg_sale_per_customer":0}]
C[{"region":"East","avg_sale_per_customer":100},{"region":"North","avg_sale_per_customer":50},{"region":"West","avg_sale_per_customer":"Infinity"}]
D[{"region":"East","avg_sale_per_customer":100},{"region":"North","avg_sale_per_customer":50},{"region":"West","avg_sale_per_customer":500}]
Attempts:
2 left
💡 Hint
NULLIF returns NULL if the second argument matches the first, preventing division by zero.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in COALESCE usage
Which of the following SQL statements will cause a syntax error?
ASELECT COALESCE(NULL, NULL, 'fallback') AS result;
BSELECT COALESCE('value1', 'value2', NULL) AS result;
CSELECT COALESCE(NULL, 'default') AS result;
DSELECT COALESCE(NULL 'default') AS result;
Attempts:
2 left
💡 Hint
Check the commas separating arguments in COALESCE.
🧠 Conceptual
advanced
1:30remaining
Understanding NULLIF behavior
What is the result of the expression NULLIF('abc', 'abc') in SQL?
A'abc'
BError
CNULL
D'' (empty string)
Attempts:
2 left
💡 Hint
NULLIF returns NULL if both arguments are equal.
optimization
expert
2:30remaining
Optimizing CASE with COALESCE
You want to replace this CASE statement with a simpler expression:
SELECT CASE WHEN col1 IS NOT NULL THEN col1 ELSE col2 END AS result FROM table1;

Which of the following is the best equivalent SQL expression?
ASELECT COALESCE(col1, col2) AS result FROM table1;
BSELECT NULLIF(col1, col2) AS result FROM table1;
CSELECT CASE WHEN col1 = col2 THEN col1 ELSE col2 END AS result FROM table1;
DSELECT IFNULL(col2, col1) AS result FROM table1;
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value.