Bird
Raised Fist0
SQLquery~10 mins

COALESCE and NULLIF as CASE shortcuts in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - COALESCE and NULLIF as CASE shortcuts
Start
↓
Evaluate NULLIF(expr1, expr2)
↓
If expr1 = expr2 -> NULL
↓
Return NULL
↓
Evaluate COALESCE(val1, val2, ..., valN)
↓
Check each val in order
↓
Return first non-NULL val
↓
End
NULLIF returns NULL if two expressions are equal, else the first expression. COALESCE returns the first non-NULL value from a list.
Execution Sample
SQL
SELECT NULLIF(5, 5) AS result1, NULLIF(5, 3) AS result2;
SELECT COALESCE(NULL, NULL, 'apple', 'banana') AS fruit;
Shows NULLIF returning NULL or value, and COALESCE returning first non-NULL value.
Execution Table
StepExpressionEvaluationResult
1NULLIF(5, 5)5 = 5 is TrueNULL
2NULLIF(5, 3)5 = 3 is False5
3COALESCE(NULL, NULL, 'apple', 'banana')Check NULL -> NULL, NULL -> NULL, 'apple' -> not NULL'apple'
4End of evaluationAll expressions processedResults returned
💡 All expressions evaluated; NULLIF returns NULL if equal, else first value; COALESCE returns first non-NULL.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
NULLIF(5,5)5,5NULLNULLNULLNULL
NULLIF(5,3)5,35,3555
COALESCE(NULL, NULL, 'apple', 'banana')NULL,NULL,'apple','banana'NULL,NULL,'apple','banana'NULL,NULL,'apple','banana''apple''apple'
Key Moments - 3 Insights
Why does NULLIF(5,5) return NULL instead of 5?
Because NULLIF returns NULL when both expressions are equal, as shown in execution_table step 1.
How does COALESCE decide which value to return?
COALESCE checks values in order and returns the first one that is not NULL, as seen in execution_table step 3.
Can COALESCE return NULL?
Yes, if all values are NULL, COALESCE returns NULL. This is implied by the checking process in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of NULLIF(5,3) at step 2?
A5
B3
CNULL
DError
💡 Hint
Refer to execution_table row 2 under Result column.
At which step does COALESCE return its final value?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Check execution_table row 3 where COALESCE finds first non-NULL.
If COALESCE(NULL, NULL, NULL) was evaluated, what would be the result?
AError
BFirst NULL
CNULL
DEmpty string
💡 Hint
COALESCE returns first non-NULL or NULL if none found, see variable_tracker for COALESCE behavior.
Concept Snapshot
NULLIF(expr1, expr2) returns NULL if expr1 = expr2, else expr1.
COALESCE(val1, val2, ..., valN) returns the first non-NULL value.
Both simplify CASE statements for common NULL logic.
Use NULLIF to avoid explicit CASE for equality checks.
Use COALESCE to pick first available non-NULL value from list.
Full Transcript
This visual execution trace shows how NULLIF and COALESCE work as shortcuts for CASE statements in SQL. NULLIF compares two expressions and returns NULL if they are equal, otherwise it returns the first expression. COALESCE takes multiple values and returns the first one that is not NULL. The execution table walks through examples: NULLIF(5,5) returns NULL because 5 equals 5; NULLIF(5,3) returns 5 because 5 does not equal 3; COALESCE(NULL, NULL, 'apple', 'banana') returns 'apple' as it is the first non-NULL value. The variable tracker shows how values change step by step. Key moments clarify common confusions like why NULLIF returns NULL when equal and how COALESCE picks values. The quiz tests understanding of these steps. The snapshot summarizes syntax and behavior for quick reference.

Practice

(1/5)
1. What does the SQL function COALESCE do?
easy
A. Returns the first non-NULL value from its arguments
B. Returns NULL if two values are equal
C. Always returns the last value in the list
D. Converts NULL values to zero

Solution

  1. Step 1: Understand COALESCE behavior

    COALESCE checks each argument in order and returns the first one that is not NULL.
  2. Step 2: Compare with other options

    NULLIF returns NULL if two values are equal, which is different. COALESCE does not convert NULL to zero automatically.
  3. Final Answer:

    Returns the first non-NULL value from its arguments -> Option A
  4. Quick Check:

    COALESCE = first non-NULL [OK]
Hint: COALESCE picks first non-NULL value in order [OK]
Common Mistakes:
  • Confusing COALESCE with NULLIF
  • Thinking COALESCE converts NULL to zero
  • Assuming COALESCE returns last value
2. Which of the following is the correct syntax for using NULLIF in SQL?
easy
A. NULLIF(value1, value2)
B. NULLIF(value1 = value2)
C. NULLIF(value1; value2)
D. NULLIF(value1, value2, value3)

Solution

  1. Step 1: Recall NULLIF syntax

    NULLIF takes exactly two arguments separated by a comma: NULLIF(value1, value2).
  2. Step 2: Eliminate incorrect options

    NULLIF(value1 = value2) uses an equality inside parentheses, which is invalid syntax. NULLIF(value1; value2) uses a semicolon instead of a comma. NULLIF(value1, value2, value3) has three arguments, which is not allowed.
  3. Final Answer:

    NULLIF(value1, value2) -> Option A
  4. Quick Check:

    NULLIF syntax = NULLIF(val1, val2) [OK]
Hint: NULLIF takes exactly two comma-separated values [OK]
Common Mistakes:
  • Using semicolon instead of comma
  • Passing more than two arguments
  • Using an expression inside NULLIF parentheses
3. Given the table Employees with columns id, bonus (nullable), what is the result of this query?
SELECT id, COALESCE(bonus, 100) AS bonus_amount FROM Employees WHERE id = 1;

Assuming the row with id=1 has bonus = NULL.
medium
A. No rows returned
B. [{"id": 1, "bonus_amount": 100}]
C. [{ "id": 1, "bonus_amount": 0 }]
D. [{ "id": 1, "bonus_amount": NULL }]

Solution

  1. Step 1: Understand COALESCE with NULL bonus

    Since bonus is NULL for id=1, COALESCE returns the second argument, 100.
  2. Step 2: Check query output

    The query selects id=1 and bonus_amount as 100, so the result is [{"id": 1, "bonus_amount": 100}].
  3. Final Answer:

    [{"id": 1, "bonus_amount": 100}] -> Option B
  4. Quick Check:

    COALESCE(NULL, 100) = 100 [OK]
Hint: COALESCE returns first non-NULL, here 100 [OK]
Common Mistakes:
  • Assuming NULL is returned instead of 100
  • Confusing COALESCE with NULLIF
  • Expecting zero instead of 100
4. Identify the error in this SQL snippet:
SELECT id, NULLIF(bonus) AS result FROM Employees;
medium
A. Missing WHERE clause
B. NULLIF cannot be used in SELECT clause
C. NULLIF requires two arguments, only one given
D. NULLIF returns wrong data type

Solution

  1. Step 1: Check NULLIF function usage

    NULLIF requires exactly two arguments, but only one (bonus) is provided here.
  2. Step 2: Validate other options

    NULLIF can be used in SELECT. WHERE clause is optional. NULLIF does not cause data type errors by itself.
  3. Final Answer:

    NULLIF requires two arguments, only one given -> Option C
  4. Quick Check:

    NULLIF needs two values [OK]
Hint: NULLIF always needs two arguments [OK]
Common Mistakes:
  • Using NULLIF with one argument
  • Thinking WHERE clause is mandatory
  • Assuming NULLIF changes data type
5. You want to replace a value with NULL if it equals zero, otherwise keep it as is. Which SQL expression using NULLIF achieves this?
hard
A. CASE WHEN value = 0 THEN value ELSE NULL END
B. COALESCE(value, 0)
C. NULLIF(0, value)
D. NULLIF(value, 0)

Solution

  1. Step 1: Understand NULLIF behavior

    NULLIF(value, 0) returns NULL if value equals 0, else returns value unchanged.
  2. Step 2: Compare other options

    COALESCE returns first non-NULL, not conditional replacement. CASE in CASE WHEN value = 0 THEN value ELSE NULL END returns value when zero, opposite of requirement. NULLIF(0, value) returns NULL if 0 equals value, which is reversed logic.
  3. Final Answer:

    NULLIF(value, 0) -> Option D
  4. Quick Check:

    NULLIF(value, 0) replaces zero with NULL [OK]
Hint: NULLIF(value, 0) returns NULL if value is zero [OK]
Common Mistakes:
  • Using COALESCE instead of NULLIF
  • Reversing argument order in NULLIF
  • Writing CASE with wrong THEN/ELSE logic
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