0
0
MySQLquery~10 mins

CASE WHEN expression in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - CASE WHEN expression
Start
Evaluate CASE expression?
NoEvaluate WHEN conditions in order
Check WHEN condition 1
True?
YesReturn THEN result 1
Compare CASE value
Match found?
NoCheck next WHEN condition
No WHEN matched?
Return THEN result
End
The CASE WHEN expression checks conditions in order and returns the THEN result of the first true condition or ELSE if none match.
Execution Sample
MySQL
SELECT name,
  CASE
    WHEN score >= 90 THEN 'A'
    WHEN score >= 80 THEN 'B'
    ELSE 'C'
  END AS grade
FROM students;
This query assigns grades A, B, or C based on the score for each student.
Execution Table
StepRow (name, score)Condition CheckedCondition ResultActionOutput (grade)
1(Alice, 95)score >= 90TrueReturn 'A'A
2(Bob, 85)score >= 90FalseCheck next WHEN
3(Bob, 85)score >= 80TrueReturn 'B'B
4(Charlie, 75)score >= 90FalseCheck next WHEN
5(Charlie, 75)score >= 80FalseCheck next WHEN
6(Charlie, 75)No more WHENN/AReturn ELSE 'C'C
7End of rowsN/AN/AQuery complete
💡 All rows processed; CASE returned first matching THEN or ELSE result.
Variable Tracker
VariableStartAfter AliceAfter BobAfter CharlieFinal
gradeNULLABCC
Key Moments - 2 Insights
Why does the CASE stop checking conditions after the first true WHEN?
Because CASE returns the THEN result immediately when a WHEN condition is true, as shown in rows 1 and 3 of the execution_table.
What happens if none of the WHEN conditions are true and there is no ELSE?
CASE returns NULL if no WHEN matches and ELSE is missing, similar to row 6 but with ELSE replaced by NULL.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what grade does Bob get at step 3?
A'A'
B'B'
C'C'
DNULL
💡 Hint
Check the Output (grade) column at step 3 in the execution_table.
At which step does the CASE expression return the ELSE result?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look for the row where 'Return ELSE' is the action in the execution_table.
If Alice's score was 85 instead of 95, what would her grade be?
A'B'
B'A'
C'C'
DNULL
💡 Hint
Refer to how Bob's score 85 results in grade 'B' in the execution_table.
Concept Snapshot
CASE WHEN expression syntax:
CASE
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ELSE default_result
END

Checks conditions in order, returns first THEN result where condition is true, else ELSE or NULL.
Full Transcript
The CASE WHEN expression in SQL evaluates conditions one by one. For each row, it checks the first WHEN condition; if true, it returns the corresponding THEN result immediately. If false, it moves to the next WHEN. If no WHEN conditions are true, it returns the ELSE result if provided, or NULL otherwise. This flow ensures only one result per row. The example query assigns grades based on score ranges. The execution table shows step-by-step how each row's score is checked and what grade is assigned. Variables track the grade assigned after each row. Key moments clarify why CASE stops after the first true condition and what happens if ELSE is missing. The quiz tests understanding of these steps and outcomes.