Challenge - 5 Problems
Nested CASE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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);
Attempts:
2 left
💡 Hint
Look carefully at the nested CASE conditions and salary value for id 3.
✗ Incorrect
The outer CASE checks if department is 'Sales'. For id 3, it is 'Sales'. Then the inner CASE checks if salary > 5000. Salary is 4500, so it returns 'Low Sales'.
📝 Syntax
intermediate1: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;
Attempts:
2 left
💡 Hint
Count the number of CASE and END keywords carefully.
✗ Incorrect
Both inner and outer CASE expressions have matching THEN and END keywords. The syntax is correct.
❓ optimization
advanced2:30remaining
Optimizing Nested CASE Expressions
You have this nested CASE expression:
Which option is the most efficient equivalent 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?
Attempts:
2 left
💡 Hint
Try to combine conditions to avoid nested CASE.
✗ Incorrect
Option A combines the nested CASE into a single CASE with combined conditions, reducing complexity and improving readability.
🔧 Debug
advanced2:30remaining
Debugging Unexpected Output from Nested CASE
Given this query:
The output is:
But the region for id 5 is 'North' and sales is 1200. What is the likely cause?
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?
Attempts:
2 left
💡 Hint
Check column names carefully and case sensitivity.
✗ Incorrect
If 'region' column is misspelled or missing, the CASE condition fails and ELSE 'Unknown' is returned.
🧠 Conceptual
expert3:00remaining
Understanding Nested CASE Evaluation Order
Consider this nested CASE expression:
Which statement best describes how SQL evaluates this 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?
Attempts:
2 left
💡 Hint
Think about how nested CASE expressions work step-by-step.
✗ Incorrect
The outer CASE checks condition1 first. Only if condition1 is true does it evaluate the inner CASE with condition2. Otherwise, it returns the ELSE value of the outer CASE.