0
0
MySQLquery~5 mins

IFNULL and COALESCE in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A'default'
BNULL
CError
DEmpty string
Which function can take more than two arguments to check for the first non-NULL value?
ACOALESCE
BIFNULL
CISNULL
DNVL
What does COALESCE(NULL, NULL, 'apple', 'banana') return?
ANULL
B'apple'
C'banana'
DError
If salary is 5000, what does IFNULL(salary, 0) return?
AError
BNULL
C0
D5000
Which function is standard SQL and works in many databases?
ANVL
BIFNULL
CCOALESCE
DISNULL
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.