Searched CASE syntax in SQL - Time & Space Complexity
We want to understand how the time it takes to run a searched CASE statement changes as the data grows.
How does the number of rows affect the work done inside the CASE?
Analyze the time complexity of the following code snippet.
SELECT
employee_id,
salary,
CASE
WHEN salary < 30000 THEN 'Low'
WHEN salary < 70000 THEN 'Medium'
ELSE 'High'
END AS salary_level
FROM employees;
This code classifies each employee's salary into categories using searched CASE conditions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The CASE conditions are checked once for each row in the employees table.
- How many times: Once per row, so as many times as there are rows.
Each row requires checking the CASE conditions in order, so the total work grows with the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks of CASE conditions |
| 100 | About 100 checks of CASE conditions |
| 1000 | About 1000 checks of CASE conditions |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles the checks.
Time Complexity: O(n)
This means the time to run the query grows in a straight line with the number of rows.
[X] Wrong: "The CASE statement runs only once regardless of rows."
[OK] Correct: The CASE is evaluated for every row, so the total work depends on how many rows there are.
Understanding how CASE statements scale helps you explain query performance clearly and shows you know how SQL processes data row by row.
"What if we added more WHEN conditions inside the CASE? How would the time complexity change?"
Practice
searched CASE expression in SQL do?Solution
Step 1: Understand the purpose of searched CASE
The searched CASE expression evaluates multiple conditions and returns a result for the first true condition.Step 2: Compare with other SQL operations
Unlike joins, deletes, or table creation, CASE is used to return values conditionally within queries.Final Answer:
Returns different results based on multiple conditions -> Option DQuick Check:
Searched CASE = conditional results [OK]
- Confusing CASE with JOIN operations
- Thinking CASE modifies tables
- Assuming CASE deletes data
Solution
Step 1: Recall the searched CASE syntax
The correct syntax starts with CASE, followed by WHEN condition THEN result, optional ELSE, and ends with END.Step 2: Check each option
Only CASE WHEN condition THEN result ELSE default END follows the correct order: CASE WHEN condition THEN result ELSE default END.Final Answer:
CASE WHEN condition THEN result ELSE default END -> Option AQuick Check:
Syntax = CASE WHEN ... THEN ... ELSE ... END [OK]
- Mixing order of WHEN and THEN
- Omitting END keyword
- Placing ELSE before THEN
Employees with column Salary, what will this query return?SELECT Salary, CASE WHEN Salary > 5000 THEN 'High' WHEN Salary > 3000 THEN 'Medium' ELSE 'Low' END AS SalaryLevel FROM Employees;
Solution
Step 1: Analyze the CASE conditions
The CASE checks if Salary > 5000, then returns 'High'; else if Salary > 3000, returns 'Medium'; otherwise 'Low'.Step 2: Understand the query output
Each row shows Salary and its label based on these conditions, so salaries are categorized correctly.Final Answer:
Labels each salary as 'High', 'Medium', or 'Low' based on value -> Option CQuick Check:
CASE categorizes salaries correctly [OK]
- Thinking ELSE is required and missing causes error
- Assuming query filters rows instead of labeling
- Ignoring order of WHEN conditions
SELECT Name, CASE WHEN Age > 18 THEN 'Adult' WHEN Age < 18 THEN 'Minor' END AS AgeGroup FROM People;
Solution
Step 1: Check CASE syntax completeness
The CASE has WHEN conditions, THEN results, and ends with END keyword properly.Step 2: Consider ELSE clause necessity
ELSE is optional; if no ELSE, NULL is returned for unmatched cases, which is valid.Final Answer:
No error, query is correct -> Option BQuick Check:
ELSE optional, END required, syntax correct [OK]
- Assuming ELSE is always required
- Forgetting END keyword
- Misplacing THEN keyword
Solution
Step 1: Match conditions to price ranges
Price < 50 should be 'Cheap', 50 to 150 'Affordable', above 150 'Expensive'.Step 2: Verify each option's logic
SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products; correctly orders conditions and labels prices as required. Others mislabel or misorder conditions.Final Answer:
SELECT ProductName, CASE WHEN Price < 50 THEN 'Cheap' WHEN Price BETWEEN 50 AND 150 THEN 'Affordable' ELSE 'Expensive' END AS PriceRange FROM Products; -> Option AQuick Check:
Conditions match price ranges correctly [OK]
- Mixing condition order causing wrong labels
- Using wrong comparison operators
- Misplacing BETWEEN ranges
Ready to prove it?
Pass the test and this pattern moves unknown
