Bird
Raised Fist0
MySQLquery~10 mins

IFNULL and COALESCE in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - IFNULL and COALESCE
Start with expression
↓
Check if first value is NULL
|Yes↓
Return second value
↓
End
No↓
Return first value
↓
End
The flow checks if the first value is NULL; if yes, it returns the next value, otherwise returns the first value.
Execution Sample
MySQL
SELECT IFNULL(NULL, 'fallback') AS result1,
       COALESCE(NULL, NULL, 'fallback', 'other') AS result2;
This query returns the first non-NULL value using IFNULL and COALESCE functions.
Execution Table
StepFunctionInput ValuesCheckReturned Value
1IFNULLNULL, 'fallback'First value is NULL'fallback'
2COALESCENULL, NULL, 'fallback', 'other'First two NULL, third not NULL'fallback'
3End--Query returns result1='fallback', result2='fallback'
💡 All functions return the first non-NULL value or NULL if all are NULL.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
IFNULL_resultNULL'fallback''fallback''fallback'
COALESCE_resultNULLNULL'fallback''fallback'
Key Moments - 2 Insights
Why does IFNULL return the second value when the first is NULL?
Because IFNULL checks if the first value is NULL (see execution_table step 1). If yes, it returns the second value as a fallback.
How does COALESCE handle multiple NULL values before a non-NULL?
COALESCE checks each value in order (execution_table step 2). It skips NULLs until it finds the first non-NULL, then returns it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does IFNULL return when the first value is NULL?
AThe first value
BNULL
CThe second value
DAn error
💡 Hint
Check execution_table row 1 under 'Returned Value'
At which step does COALESCE return its value?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at execution_table row 2 for COALESCE's returned value
If all values in COALESCE were NULL, what would it return?
ANULL
BThe last NULL
CThe first NULL
DAn error
💡 Hint
COALESCE returns the first non-NULL or NULL if none found (see exit_note)
Concept Snapshot
IFNULL(expr1, expr2): returns expr2 if expr1 is NULL, else expr1.
COALESCE(expr1, expr2, ...): returns the first non-NULL expr.
Both help handle NULL values by providing fallback.
IFNULL takes exactly two arguments; COALESCE can take many.
Useful to avoid NULL results in queries.
Full Transcript
This lesson shows how IFNULL and COALESCE work in MySQL. IFNULL checks if the first value is NULL; if yes, it returns the second value. COALESCE checks multiple values in order and returns the first one that is not NULL. The example query uses IFNULL with NULL and 'fallback' and COALESCE with several NULLs before 'fallback'. The execution table shows step-by-step how each function returns 'fallback' as the first non-NULL value. Key moments explain why IFNULL returns the second value when the first is NULL and how COALESCE skips NULLs until it finds a non-NULL. The quiz tests understanding of these steps. Remember, IFNULL is for two values, COALESCE for many, both help avoid NULL results in your data.

Practice

(1/5)
1. What does the IFNULL(expr1, expr2) function do in MySQL?
easy
A. Checks if expr1 equals expr2 and returns TRUE or FALSE.
B. Returns expr2 if expr1 is NULL, otherwise returns expr1.
C. Returns the first non-NULL value from a list of expressions.
D. Returns NULL if expr1 is NULL, otherwise returns expr2.

Solution

  1. Step 1: Understand IFNULL function behavior

    IFNULL takes two expressions and checks if the first is NULL.
  2. Step 2: Determine return value based on NULL check

    If the first expression is NULL, it returns the second; otherwise, it returns the first.
  3. Final Answer:

    Returns expr2 if expr1 is NULL, otherwise returns expr1. -> Option B
  4. Quick Check:

    IFNULL(expr1, expr2) = expr2 if expr1 is NULL [OK]
Hint: IFNULL replaces NULL with one default value quickly [OK]
Common Mistakes:
  • Confusing IFNULL with COALESCE
  • Thinking IFNULL returns NULL always
  • Mixing order of arguments
2. Which of the following is the correct syntax to use COALESCE in MySQL?
easy
A. COALESCE(expr1, expr2, ..., exprN)
B. COALESCE(expr1; expr2; ...; exprN)
C. COALESCE[expr1, expr2, ..., exprN]
D. COALESCE{expr1, expr2, ..., exprN}

Solution

  1. Step 1: Recall COALESCE syntax

    COALESCE takes a comma-separated list of expressions inside parentheses.
  2. Step 2: Identify correct punctuation

    Only COALESCE(expr1, expr2, ..., exprN) uses commas and parentheses correctly.
  3. Final Answer:

    COALESCE(expr1, expr2, ..., exprN) -> Option A
  4. Quick Check:

    COALESCE uses commas and parentheses [OK]
Hint: Use commas inside parentheses for COALESCE [OK]
Common Mistakes:
  • Using semicolons instead of commas
  • Using brackets or braces instead of parentheses
  • Missing parentheses
3. Given the table users with columns id, nickname, and fullname, what will this query return?
SELECT id, COALESCE(nickname, fullname, 'Guest') AS display_name FROM users;

Assuming a row has nickname = NULL and fullname = 'Alice'.
medium
A. 'Alice'
B. 'Guest'
C. NULL
D. Empty string

Solution

  1. Step 1: Understand COALESCE behavior with given values

    COALESCE returns the first non-NULL value from the list: nickname, fullname, 'Guest'.
  2. Step 2: Apply values to COALESCE

    Since nickname is NULL, it checks fullname which is 'Alice', so it returns 'Alice'.
  3. Final Answer:

    'Alice' -> Option A
  4. Quick Check:

    COALESCE(NULL, 'Alice', 'Guest') = 'Alice' [OK]
Hint: COALESCE picks first non-NULL value from left to right [OK]
Common Mistakes:
  • Assuming COALESCE returns NULL if first is NULL
  • Confusing COALESCE with IFNULL
  • Thinking it returns default always
4. Identify the error in this query:
SELECT IFNULL(name, 'Unknown' 'N/A') FROM employees;
medium
A. No error, query is correct
B. IFNULL cannot take string literals
C. Incorrect function name, should be COALESCE
D. Missing comma between 'Unknown' and 'N/A'

Solution

  1. Step 1: Check IFNULL syntax

    IFNULL takes exactly two arguments separated by a comma.
  2. Step 2: Identify argument separation error

    The query has two string literals without a comma between them, causing syntax error.
  3. Final Answer:

    Missing comma between 'Unknown' and 'N/A' -> Option D
  4. Quick Check:

    Arguments must be comma-separated [OK]
Hint: Separate arguments with commas in functions [OK]
Common Mistakes:
  • Forgetting commas between arguments
  • Using IFNULL with more than two arguments
  • Confusing IFNULL and COALESCE syntax
5. You want to display a user's contact info by checking email, then phone, and if both are NULL, show 'No contact'. Which query correctly uses COALESCE for this?
hard
A. SELECT COALESCE(email, 'No contact', phone) AS contact_info FROM users;
B. SELECT IFNULL(email, IFNULL(phone, 'No contact')) AS contact_info FROM users;
C. SELECT COALESCE(email, phone, 'No contact') AS contact_info FROM users;
D. SELECT IFNULL(email, phone, 'No contact') AS contact_info FROM users;

Solution

  1. Step 1: Understand COALESCE usage for multiple values

    COALESCE returns the first non-NULL value from all arguments in order.
  2. Step 2: Check argument order for contact info

    We want email first, then phone, then 'No contact' if both NULL.
  3. Step 3: Validate each option

    SELECT COALESCE(email, phone, 'No contact') AS contact_info FROM users; correctly orders arguments. SELECT IFNULL(email, IFNULL(phone, 'No contact')) AS contact_info FROM users; uses nested IFNULL which works but is more complex. SELECT COALESCE(email, 'No contact', phone) AS contact_info FROM users; puts 'No contact' before phone, which is wrong. SELECT IFNULL(email, phone, 'No contact') AS contact_info FROM users; has wrong IFNULL syntax with three arguments.
  4. Final Answer:

    SELECT COALESCE(email, phone, 'No contact') AS contact_info FROM users; -> Option C
  5. Quick Check:

    COALESCE picks first non-NULL in order [OK]
Hint: List values in COALESCE from most to least preferred [OK]
Common Mistakes:
  • Putting default value before other columns
  • Using IFNULL with more than two arguments
  • Confusing argument order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard