0
0
SQLquery~5 mins

Nested CASE expressions in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a CASE expression in SQL?
A CASE expression lets you perform conditional logic in SQL queries. It works like an IF-THEN-ELSE statement, returning different values based on conditions.
Click to reveal answer
intermediate
What does it mean to have a nested CASE expression?
A nested CASE expression is when you put one CASE expression inside another. This helps handle multiple layers of conditions in a query.
Click to reveal answer
intermediate
Write a simple example of a nested CASE expression in SQL.
SELECT CASE WHEN score >= 90 THEN 'A' WHEN score >= 80 THEN CASE WHEN score >= 85 THEN 'B+' ELSE 'B' END ELSE 'C' END AS grade FROM students;
Click to reveal answer
intermediate
Why use nested CASE expressions instead of multiple separate CASE statements?
Nested CASE expressions keep related conditions together, making queries easier to read and maintain. They also allow more precise control over complex decision logic.
Click to reveal answer
advanced
Can a nested CASE expression return different data types in its branches?
No. All return values in a CASE expression, including nested ones, should be of compatible data types to avoid errors.
Click to reveal answer
What keyword starts a CASE expression in SQL?
ACASE
BIF
CWHEN
DSELECT
In a nested CASE, where is the inner CASE placed?
AAfter the END keyword
BBefore the outer CASE
CInside the THEN or ELSE part of the outer CASE
DOutside the SELECT statement
What happens if none of the WHEN conditions in a CASE expression match and there is no ELSE?
AReturns 0
BReturns NULL
CReturns an error
DReturns the first WHEN value
Which of these is a valid use of nested CASE?
ACASE WHEN x > 10 THEN 'High' CASE WHEN x > 20 THEN 'Very High' END ELSE 'Low' END
BCASE x > 10 THEN 'High' ELSE 'Low' END
CCASE WHEN x > 10 THEN 'High' ELSE CASE WHEN x > 20 THEN 'Very High' END
DCASE WHEN x > 10 THEN CASE WHEN x > 20 THEN 'High' ELSE 'Medium' END ELSE 'Low' END
Can nested CASE expressions be used in the SELECT clause?
AYes, to compute complex conditional values
BNo, only in WHERE clauses
CNo, only in ORDER BY clauses
DOnly in JOIN conditions
Explain how a nested CASE expression works in SQL and give a simple example.
Think of CASE inside CASE like decision steps inside other decision steps.
You got /3 concepts.
    Describe why nested CASE expressions are useful and what to watch out for when using them.
    Consider both benefits and challenges of nesting conditions.
    You got /3 concepts.