0
0
SQLquery~20 mins

Nested CASE expressions in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nested CASE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of Nested CASE in SELECT
Given the table Employees with columns id, department, and salary, what is the output of this query?
SELECT id, department, salary,
CASE
WHEN department = 'Sales' THEN
CASE WHEN salary > 5000 THEN 'High Sales' ELSE 'Low Sales' END
ELSE 'Other' END AS category
FROM Employees
WHERE id = 3;
SQL
CREATE TABLE Employees (id INT, department VARCHAR(20), salary INT);
INSERT INTO Employees VALUES (1, 'Sales', 6000), (2, 'HR', 4000), (3, 'Sales', 4500), (4, 'IT', 7000);
A[{"id":3,"department":"Sales","salary":4500,"category":"Low Sales"}]
B[{"id":3,"department":"Sales","salary":4500,"category":"High Sales"}]
C[]
D[{"id":3,"department":"Sales","salary":4500,"category":"Other"}]
Attempts:
2 left
💡 Hint
Look carefully at the nested CASE conditions and salary value for id 3.
📝 Syntax
intermediate
1:30remaining
Identify the Syntax Error in Nested CASE
Which option contains a syntax error in the nested CASE expression below?
SELECT id,
CASE WHEN score > 80 THEN
CASE WHEN grade = 'A' THEN 'Excellent'
ELSE 'Good' END
ELSE 'Average' END AS performance
FROM Results;
AMissing END for outer CASE
BMissing THEN keyword in inner CASE
CAll syntax is correct
DMissing END for inner CASE
Attempts:
2 left
💡 Hint
Count the number of CASE and END keywords carefully.
optimization
advanced
2:30remaining
Optimizing Nested CASE Expressions
You have this nested CASE expression:
CASE
WHEN status = 'Active' THEN
CASE WHEN score >= 90 THEN 'Top Performer' ELSE 'Active Member' END
WHEN status = 'Inactive' THEN 'No Access'
ELSE 'Unknown' END

Which option is the most efficient equivalent expression?
ACASE WHEN status = 'Active' AND score >= 90 THEN 'Top Performer' WHEN status = 'Active' THEN 'Active Member' WHEN status = 'Inactive' THEN 'No Access' ELSE 'Unknown' END
BCASE WHEN score >= 90 THEN 'Top Performer' WHEN status = 'Active' THEN 'Active Member' WHEN status = 'Inactive' THEN 'No Access' ELSE 'Unknown' END
CCASE WHEN status = 'Active' THEN 'Active Member' WHEN score >= 90 THEN 'Top Performer' WHEN status = 'Inactive' THEN 'No Access' ELSE 'Unknown' END
DCASE WHEN status = 'Inactive' THEN 'No Access' WHEN status = 'Active' THEN CASE WHEN score >= 90 THEN 'Top Performer' ELSE 'Active Member' END ELSE 'Unknown' END
Attempts:
2 left
💡 Hint
Try to combine conditions to avoid nested CASE.
🔧 Debug
advanced
2:30remaining
Debugging Unexpected Output from Nested CASE
Given this query:
SELECT id,
CASE WHEN region = 'North' THEN
CASE WHEN sales > 1000 THEN 'High' ELSE 'Low' END
ELSE 'Unknown' END AS sales_level
FROM SalesData
WHERE id = 5;

The output is:
{"id":5,"sales_level":"Unknown"}

But the region for id 5 is 'North' and sales is 1200. What is the likely cause?
AThe sales value is stored as text, causing comparison failure
BThe column name 'region' is misspelled or does not exist
CThe WHERE clause filters out the row with id 5
DThe nested CASE syntax is incorrect
Attempts:
2 left
💡 Hint
Check column names carefully and case sensitivity.
🧠 Conceptual
expert
3:00remaining
Understanding Nested CASE Evaluation Order
Consider this nested CASE expression:
CASE
WHEN condition1 THEN
CASE
WHEN condition2 THEN 'Result A'
ELSE 'Result B'
END
ELSE 'Result C'
END

Which statement best describes how SQL evaluates this expression?
ASQL evaluates condition2 first; if true, then evaluates condition1; otherwise returns 'Result C'
BSQL evaluates condition1 and condition2 independently and concatenates results
CSQL evaluates both condition1 and condition2 simultaneously and returns the first true result
DSQL evaluates condition1 first; if true, then evaluates condition2; otherwise returns 'Result C'
Attempts:
2 left
💡 Hint
Think about how nested CASE expressions work step-by-step.