0
0
MySQLquery~20 mins

IFNULL and COALESCE in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IFNULL and COALESCE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A100
B0
CNULL
DError
Attempts:
2 left
💡 Hint
IFNULL returns the first argument if it is not NULL, otherwise the second.
query_result
intermediate
2: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;
ANULL
B15
C0
DError
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value from its arguments.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in COALESCE usage
Which option contains a syntax error when using COALESCE in MySQL?
ASELECT COALESCE();
BSELECT COALESCE(NULL, NULL, 0);
CSELECT COALESCE(NULL, 5, 10);
DSELECT COALESCE('a', 'b');
Attempts:
2 left
💡 Hint
COALESCE requires at least one argument.
query_result
advanced
2:00remaining
Difference between IFNULL and COALESCE output
Given the query:
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;
Aval1 = NULL, val2 = 0
Bval1 = NULL, val2 = NULL
Cval1 = 0, val2 = NULL
Dval1 = 0, val2 = 0
Attempts:
2 left
💡 Hint
Both IFNULL and COALESCE return the first non-NULL argument.
🧠 Conceptual
expert
2:00remaining
Behavior of COALESCE with subqueries returning NULL
Consider this query:
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;
AError due to subqueries
BNULL
C42
DEmpty result set
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value among its arguments, including subquery results.