Recall & Review
beginner
What does the IFNULL function do in MySQL?
IFNULL(expression, alternative) returns the alternative value if the expression is NULL; otherwise, it returns the expression's value.
Click to reveal answer
intermediate
How is COALESCE different from IFNULL?
COALESCE(expression1, expression2, ..., expressionN) returns the first non-NULL expression from the list, while IFNULL only takes two arguments.
Click to reveal answer
beginner
Write a simple example using IFNULL to replace NULL with 0.
SELECT IFNULL(salary, 0) AS salary FROM employees;
Click to reveal answer
beginner
Write a simple example using COALESCE to find the first non-NULL value among three columns.
SELECT COALESCE(phone_home, phone_mobile, phone_work) AS contact_phone FROM contacts;
Click to reveal answer
intermediate
Why might you prefer COALESCE over IFNULL in some cases?
Because COALESCE can check multiple values and return the first non-NULL, it is more flexible for handling several possible NULLs.
Click to reveal answer
What will IFNULL(NULL, 'default') return?
✗ Incorrect
IFNULL returns the second argument if the first is NULL.
Which function can take more than two arguments to check for the first non-NULL value?
✗ Incorrect
COALESCE accepts multiple arguments and returns the first non-NULL.
What does COALESCE(NULL, NULL, 'apple', 'banana') return?
✗ Incorrect
COALESCE returns the first non-NULL value, which is 'apple'.
If salary is 5000, what does IFNULL(salary, 0) return?
✗ Incorrect
IFNULL returns the original value if it is not NULL.
Which function is standard SQL and works in many databases?
✗ Incorrect
COALESCE is part of the SQL standard and widely supported.
Explain how IFNULL works and give a simple example.
Think about replacing missing values with something else.
You got /3 concepts.
Describe the difference between IFNULL and COALESCE and when to use each.
Consider how many values you want to check for NULL.
You got /4 concepts.