Challenge - 5 Problems
Searched CASE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of Searched CASE with multiple conditions
Given the table Employees with columns
Assume the
id, salary, and department, what is the output of the following query?SELECT id, salary,
CASE
WHEN salary > 70000 THEN 'High'
WHEN salary >= 40000 AND salary <= 70000 THEN 'Medium'
ELSE 'Low'
END AS SalaryLevel
FROM Employees
WHERE department = 'Sales';
Assume the
Employees table has these rows:id | salary | department
1 | 80000 | Sales
2 | 50000 | Sales
3 | 30000 | Sales
4 | 60000 | HR
SQL
SELECT id, salary, CASE WHEN salary > 70000 THEN 'High' WHEN salary >= 40000 AND salary <= 70000 THEN 'Medium' ELSE 'Low' END AS SalaryLevel FROM Employees WHERE department = 'Sales';
Attempts:
2 left
💡 Hint
Remember the searched CASE evaluates conditions in order and stops at the first true condition.
✗ Incorrect
The CASE expression checks salary > 70000 first, so id=1 gets 'High'. Then salary between 40000 and 70000 is 'Medium' for id=2. The ELSE covers salaries below 40000, so id=3 gets 'Low'. Only employees in Sales department are included.
🧠 Conceptual
intermediate1:30remaining
Understanding evaluation order in Searched CASE
In a searched CASE expression, why is the order of WHEN conditions important?
Consider:
What happens if the order is changed to check
Consider:
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
ELSE 'F'
END
What happens if the order is changed to check
score >= 70 first?Attempts:
2 left
💡 Hint
Think about how CASE stops checking conditions once one is true.
✗ Incorrect
Searched CASE evaluates conditions top to bottom and returns the result of the first true condition. If a broader condition like score >= 70 is first, it matches many scores and later conditions are ignored.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this searched CASE query
Which option contains a syntax error in the searched CASE expression?
SELECT id,
CASE
WHEN age > 60 THEN 'Senior'
WHEN age >= 18 THEN 'Adult'
ELSE 'Minor'
END AS AgeGroup
FROM Users;
Attempts:
2 left
💡 Hint
Check the syntax for searched CASE vs simple CASE.
✗ Incorrect
Option D incorrectly uses searched CASE syntax with a simple CASE style (CASE age WHEN > 60 ...), which is invalid. The searched CASE requires WHEN conditions with full boolean expressions.
❓ optimization
advanced2:30remaining
Optimizing searched CASE for performance
You have a large table
Which approach can improve query performance when filtering by these categories?
Orders with a total_amount column. You want to classify orders as 'Small', 'Medium', or 'Large' using searched CASE:CASE
WHEN total_amount < 100 THEN 'Small'
WHEN total_amount < 500 THEN 'Medium'
ELSE 'Large'
END
Which approach can improve query performance when filtering by these categories?
Attempts:
2 left
💡 Hint
Think about indexing and how it helps filtering.
✗ Incorrect
Creating a computed column with the CASE expression and indexing it allows the database to quickly filter by category without recalculating CASE for every row.
🔧 Debug
expert3:00remaining
Debugging unexpected output from searched CASE
A developer writes this query:
They notice that products with stock 5 are labeled 'Low' as expected, but products with stock 30 are labeled 'Low' instead of 'Medium'. What is the most likely cause?
SELECT product_id,
CASE
WHEN stock < 10 THEN 'Low'
WHEN stock < 50 THEN 'Medium'
WHEN stock < 100 THEN 'High'
ELSE 'Very High'
END AS StockLevel
FROM Inventory;
They notice that products with stock 5 are labeled 'Low' as expected, but products with stock 30 are labeled 'Low' instead of 'Medium'. What is the most likely cause?
Attempts:
2 left
💡 Hint
Check data types and how comparisons work.
✗ Incorrect
If stock is stored as text, '30' < '10' can be false in string comparison because '3' is greater than '1' in ASCII order, causing wrong CASE branch to match.