Challenge - 5 Problems
IFNULL and COALESCE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of IFNULL with NULL value
Consider a table employees with a column
bonus that can be NULL. What is the output of this query?SELECT IFNULL(bonus, 100) AS bonus_amount FROM employees WHERE id = 1;Assuming
bonus is NULL for id = 1.MySQL
SELECT IFNULL(bonus, 100) AS bonus_amount FROM employees WHERE id = 1;
Attempts:
2 left
💡 Hint
IFNULL returns the first argument if it is not NULL, otherwise the second.
✗ Incorrect
Since the bonus is NULL, IFNULL returns the second argument, which is 100.
❓ query_result
intermediate2:00remaining
Output of COALESCE with multiple NULLs
Given a table orders with columns
discount1, discount2, and discount3, all possibly NULL. What is the output of this query?SELECT COALESCE(discount1, discount2, discount3, 0) AS discount FROM orders WHERE order_id = 5;Assuming
discount1 and discount2 are NULL, and discount3 is 15.MySQL
SELECT COALESCE(discount1, discount2, discount3, 0) AS discount FROM orders WHERE order_id = 5;
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value from its arguments.
✗ Incorrect
Since discount1 and discount2 are NULL, COALESCE returns discount3 which is 15.
📝 Syntax
advanced2:00remaining
Identify the syntax error in COALESCE usage
Which option contains a syntax error when using COALESCE in MySQL?
Attempts:
2 left
💡 Hint
COALESCE requires at least one argument.
✗ Incorrect
Option A has no arguments inside COALESCE, which is invalid syntax.
❓ query_result
advanced2:00remaining
Difference between IFNULL and COALESCE output
Given the query:
What is the output?
SELECT IFNULL(NULL, 0) AS val1, COALESCE(NULL, 0) AS val2;
What is the output?
MySQL
SELECT IFNULL(NULL, 0) AS val1, COALESCE(NULL, 0) AS val2;
Attempts:
2 left
💡 Hint
Both IFNULL and COALESCE return the first non-NULL argument.
✗ Incorrect
Both functions return 0 because the first argument is NULL and the second is 0.
🧠 Conceptual
expert2:00remaining
Behavior of COALESCE with subqueries returning NULL
Consider this query:
What is the value of
SELECT COALESCE((SELECT NULL), (SELECT NULL), 42) AS result;
What is the value of
result?MySQL
SELECT COALESCE((SELECT NULL), (SELECT NULL), 42) AS result;
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value among its arguments, including subquery results.
✗ Incorrect
Both subqueries return NULL, so COALESCE returns the last argument 42.