Bird
Raised Fist0
SQLquery~10 mins

Nested CASE expressions in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Nested CASE expressions
Start CASE
↓
Evaluate Outer CASE Condition
↓
Return
↓
Return
↓
End
The outer CASE checks a condition; if false, it evaluates an inner CASE expression before returning a result.
Execution Sample
SQL
SELECT
  CASE
    WHEN score >= 90 THEN 'A'
    ELSE CASE
      WHEN score >= 80 THEN 'B'
      ELSE 'C'
    END
  END AS grade
FROM students;
Assigns grades A, B, or C based on score using nested CASE expressions.
Execution Table
StepscoreOuter CASE Condition (score >= 90)Outer CASE ResultInner CASE Condition (score >= 80)Inner CASE ResultFinal grade
195True'A'N/AN/A'A'
285FalseEvaluate Inner CASETrue'B''B'
375FalseEvaluate Inner CASEFalse'C''C'
490True'A'N/AN/A'A'
580FalseEvaluate Inner CASETrue'B''B'
670FalseEvaluate Inner CASEFalse'C''C'
ExitN/AN/AAll rows processedN/AN/AGrades assigned
💡 All rows processed, final grades assigned based on nested CASE logic.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6Final
scoreN/A958575908070N/A
Outer CASE ResultN/A'A'Evaluate Inner CASEEvaluate Inner CASE'A'Evaluate Inner CASEEvaluate Inner CASEN/A
Inner CASE ResultN/AN/A'B''C'N/A'B''C'N/A
Final gradeN/A'A''B''C''A''B''C'N/A
Key Moments - 2 Insights
Why does the inner CASE only run when the outer CASE condition is false?
Because the outer CASE returns immediately when its condition is true (rows 1 and 4), skipping the inner CASE. The inner CASE runs only when the outer CASE condition is false (rows 2,3,5,6).
What happens if the inner CASE also has multiple conditions?
The inner CASE evaluates its conditions in order, returning the first true condition's result, just like the outer CASE. This is shown in rows 2,3,5,6 where inner CASE picks 'B' or 'C'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the final grade when score is 85 at step 2?
A'A'
B'C'
C'B'
DNULL
💡 Hint
Check row 2 in execution_table under 'Final grade' column.
At which step does the outer CASE condition become true?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at 'Outer CASE Condition' column in execution_table for 'True' values.
If the score is 88, what would the inner CASE result be?
A'A'
B'B'
C'C'
DNULL
💡 Hint
Refer to inner CASE logic and variable_tracker for scores between 80 and 89.
Concept Snapshot
Nested CASE expressions let you put one CASE inside another.
Outer CASE checks a condition first.
If false, inner CASE runs to check more conditions.
Returns the first true condition's result.
Useful for multi-level decision making in SQL.
Full Transcript
This visual execution trace shows how nested CASE expressions work in SQL. The outer CASE checks if the score is 90 or above. If yes, it returns 'A' immediately. If not, it evaluates the inner CASE which checks if the score is 80 or above to return 'B', else returns 'C'. The execution table walks through example scores step-by-step, showing which CASE runs and what grade is assigned. The variable tracker shows how values change after each step. Key moments clarify why inner CASE only runs when outer CASE condition is false and how multiple conditions are handled. The quiz tests understanding of the flow and results. The snapshot summarizes the concept simply.

Practice

(1/5)
1. What is the main purpose of using nested CASE expressions in SQL?
easy
A. To check multiple conditions inside another condition for detailed results
B. To create new tables from existing data
C. To delete rows based on conditions
D. To join two tables together

Solution

  1. Step 1: Understand the role of CASE expressions

    CASE expressions allow conditional logic to return different values based on conditions.
  2. Step 2: Recognize nested CASE usage

    Nested CASE means putting one CASE inside another to check conditions within conditions for more detailed categorization.
  3. Final Answer:

    To check multiple conditions inside another condition for detailed results -> Option A
  4. Quick Check:

    Nested CASE = detailed condition checks [OK]
Hint: Nested CASE means CASE inside CASE for detailed checks [OK]
Common Mistakes:
  • Confusing CASE with JOIN operations
  • Thinking CASE creates or deletes tables
  • Ignoring the need to close each CASE with END
2. Which of the following is the correct syntax for a nested CASE expression in SQL?
easy
A. CASE WHEN condition1 THEN IF condition2 THEN result1 ELSE result2 END ELSE result3 END
B. CASE WHEN condition1 THEN CASE condition2 THEN result1 ELSE result2 END ELSE result3 END
C. IF condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END
D. CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END

Solution

  1. Step 1: Review correct CASE syntax

    CASE expressions use WHEN ... THEN ... ELSE ... END structure; nested CASE must follow this pattern inside THEN.
  2. Step 2: Identify correct nested CASE

    CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END correctly nests a CASE inside THEN with proper WHEN, THEN, ELSE, and END keywords.
  3. Final Answer:

    CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END -> Option D
  4. Quick Check:

    Nested CASE syntax = CASE WHEN condition1 THEN CASE WHEN condition2 THEN result1 ELSE result2 END ELSE result3 END [OK]
Hint: Nested CASE uses WHEN-THEN inside THEN with END for each CASE [OK]
Common Mistakes:
  • Using IF instead of CASE inside SQL
  • Missing END keyword for inner CASE
  • Incorrect keyword order in nested CASE
3. Given the table Employees with column Salary, what will this query return?
SELECT Salary, CASE WHEN Salary > 70000 THEN CASE WHEN Salary > 90000 THEN 'High' ELSE 'Medium' END ELSE 'Low' END AS SalaryLevel FROM Employees;
medium
A. Labels all salaries above 70000 as 'High' and others as 'Low'
B. Labels salaries above 90000 as 'High', between 70001 and 90000 as 'Medium', others as 'Low'
C. Labels all salaries below 70000 as 'Medium' and others as 'High'
D. Returns an error due to incorrect CASE syntax

Solution

  1. Step 1: Analyze outer CASE condition

    If Salary > 70000, then check inner CASE; else label 'Low'.
  2. Step 2: Analyze inner CASE condition

    Inside outer WHEN true, if Salary > 90000 then 'High', else 'Medium'.
  3. Final Answer:

    Labels salaries above 90000 as 'High', between 70001 and 90000 as 'Medium', others as 'Low' -> Option B
  4. Quick Check:

    Nested CASE categorizes salary levels correctly [OK]
Hint: Check outer condition first, then inner CASE for detailed labels [OK]
Common Mistakes:
  • Ignoring inner CASE and labeling all above 70000 as same
  • Confusing syntax causing errors
  • Misreading comparison operators
4. Identify the error in this nested CASE SQL snippet:
SELECT CASE WHEN score > 80 THEN CASE score > 90 THEN 'Excellent' ELSE 'Good' END ELSE 'Average' END AS Grade FROM Results;
medium
A. Using wrong comparison operator
B. Missing END keyword for outer CASE
C. Missing WHEN keyword before inner condition
D. Incorrect ELSE placement inside inner CASE

Solution

  1. Step 1: Check inner CASE syntax

    Inner CASE lacks WHEN before condition; it should be 'CASE WHEN score > 90 THEN ...'
  2. Step 2: Verify overall CASE structure

    Outer CASE has proper WHEN and END; error is only in inner CASE missing WHEN keyword.
  3. Final Answer:

    Missing WHEN keyword before inner condition -> Option C
  4. Quick Check:

    Inner CASE needs WHEN before condition [OK]
Hint: Every CASE condition must start with WHEN [OK]
Common Mistakes:
  • Forgetting WHEN in inner CASE
  • Assuming END is missing when it is present
  • Misplacing ELSE inside CASE
5. You want to classify products based on Price and Stock using nested CASE. If Price > 100 and Stock > 50, label 'Premium-Available'. If Price > 100 and Stock ≤ 50, label 'Premium-Limited'. Otherwise, label 'Standard'. Which SQL snippet correctly implements this?
hard
A. CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END
B. CASE WHEN Price > 100 AND Stock > 50 THEN 'Premium-Available' WHEN Price > 100 AND Stock <= 50 THEN 'Premium-Limited' ELSE 'Standard' END
C. CASE WHEN Price > 100 THEN 'Premium' ELSE CASE WHEN Stock > 50 THEN 'Available' ELSE 'Limited' END END
D. CASE WHEN Price > 100 THEN CASE Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END

Solution

  1. Step 1: Understand the nested conditions

    First check if Price > 100, then inside check Stock > 50 or not.
  2. Step 2: Match correct syntax and logic

    CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END correctly nests CASE inside THEN with proper WHEN, THEN, ELSE, and END keywords matching the problem.
  3. Final Answer:

    CASE WHEN Price > 100 THEN CASE WHEN Stock > 50 THEN 'Premium-Available' ELSE 'Premium-Limited' END ELSE 'Standard' END -> Option A
  4. Quick Check:

    Nested CASE matches Price then Stock checks [OK]
Hint: Check outer condition first, nest inner CASE for second condition [OK]
Common Mistakes:
  • Using AND instead of nested CASE
  • Missing WHEN in inner CASE
  • Incorrect ELSE placement
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