0
0
SQLquery~20 mins

Searched CASE syntax in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Searched CASE Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of Searched CASE with multiple conditions
Given the table Employees with columns 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';
A[{"id":1,"salary":80000,"SalaryLevel":"High"},{"id":2,"salary":50000,"SalaryLevel":"Low"},{"id":3,"salary":30000,"SalaryLevel":"Low"}]
B[{"id":1,"salary":80000,"SalaryLevel":"High"},{"id":2,"salary":50000,"SalaryLevel":"Medium"},{"id":3,"salary":30000,"SalaryLevel":"Low"}]
C[{"id":1,"salary":80000,"SalaryLevel":"Medium"},{"id":2,"salary":50000,"SalaryLevel":"Medium"},{"id":3,"salary":30000,"SalaryLevel":"Low"}]
D[{"id":1,"salary":80000,"SalaryLevel":"High"},{"id":2,"salary":50000,"SalaryLevel":"Medium"}]
Attempts:
2 left
💡 Hint
Remember the searched CASE evaluates conditions in order and stops at the first true condition.
🧠 Conceptual
intermediate
1:30remaining
Understanding evaluation order in Searched CASE
In a searched CASE expression, why is the order of WHEN conditions important?

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?
AThe first matching condition is used, so if <code>score &gt;= 70</code> is first, all scores 70 and above get 'C', ignoring higher grades.
BThe CASE expression evaluates all conditions and returns the highest grade matching the score.
CThe CASE expression will cause a syntax error if conditions are reordered.
DThe order does not matter; all conditions are checked and the best match is returned.
Attempts:
2 left
💡 Hint
Think about how CASE stops checking conditions once one is true.
📝 Syntax
advanced
2: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;
ACASE WHEN age > 60 THEN 'Senior' WHEN age >= 18 THEN 'Adult' ELSE 'Minor' END
BCASE WHEN age > 60 THEN 'Senior' WHEN age >= 18 THEN 'Adult' ELSE 'Minor' END AS AgeGroup
CDNE 'roniM' ESLE 'tludA' NEHT 81 => ega NEHW 'roineS' NEHT 06 > ega NEHW ESAC
DCASE age WHEN > 60 THEN 'Senior' WHEN >= 18 THEN 'Adult' ELSE 'Minor' END
Attempts:
2 left
💡 Hint
Check the syntax for searched CASE vs simple CASE.
optimization
advanced
2:30remaining
Optimizing searched CASE for performance
You have a large table 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?
ARewrite the CASE as multiple OR conditions in the WHERE clause without indexing.
BUse nested CASE expressions inside the query to reduce evaluation time.
CCreate a computed column with the CASE expression and index it, then filter on that column.
DRemove the CASE expression and filter only by total_amount ranges in the WHERE clause.
Attempts:
2 left
💡 Hint
Think about indexing and how it helps filtering.
🔧 Debug
expert
3:00remaining
Debugging unexpected output from searched CASE
A developer writes this query:
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?
AThe stock column is stored as text, so comparisons behave unexpectedly.
BThe CASE conditions are evaluated in order, so stock < 10 matches first, but 30 is not less than 10, so it should be 'Medium'.
CThere is a hidden trigger changing stock values before query execution.
DThe query is missing parentheses around conditions causing wrong evaluation.
Attempts:
2 left
💡 Hint
Check data types and how comparisons work.