Simple CASE syntax in SQL - Time & Space Complexity
We want to understand how the time it takes to run a SQL query with a simple CASE changes as the data grows.
Specifically, how does the number of rows affect the work done by the CASE expression?
Analyze the time complexity of the following code snippet.
SELECT
employee_id,
salary,
CASE department_id
WHEN 10 THEN 'Accounting'
WHEN 20 THEN 'Sales'
ELSE 'Other'
END AS department_name
FROM employees;
This query assigns a department name to each employee based on their department ID using a simple CASE expression.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The CASE expression is evaluated once for each row in the employees table.
- How many times: Exactly as many times as there are rows (employees).
As the number of employees grows, the CASE expression runs once per employee.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 CASE evaluations |
| 100 | 100 CASE evaluations |
| 1000 | 1000 CASE evaluations |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles the work.
Time Complexity: O(n)
This means the time to run the CASE grows in a straight line with the number of rows.
[X] Wrong: "The CASE expression runs only once regardless of rows."
[OK] Correct: The CASE is evaluated for each row separately, so it repeats as many times as there are rows.
Understanding how simple CASE expressions scale helps you explain query performance clearly and confidently.
"What if we added a nested CASE inside the simple CASE? How would the time complexity change?"
Practice
Simple CASE expression do in SQL?Solution
Step 1: Understand the purpose of Simple CASE
Simple CASE compares one expression against multiple possible values.Step 2: Identify the result behavior
It returns the result for the first matching value it finds.Final Answer:
It compares one expression to multiple values and returns a result for the first match. -> Option BQuick Check:
Simple CASE = compare expression to values [OK]
- Thinking it loops through rows
- Confusing with IF statements
- Assuming it creates or deletes tables
Solution
Step 1: Recall Simple CASE syntax structure
The correct syntax is: CASE expression WHEN value THEN result ELSE default END.Step 2: Compare options
CASE expression WHEN value THEN result ELSE default END matches this syntax exactly, others use incorrect keywords or structure.Final Answer:
CASE expression WHEN value THEN result ELSE default END -> Option AQuick Check:
Simple CASE syntax = CASE expr WHEN val THEN res ELSE def END [OK]
- Using WHEN without expression after CASE
- Confusing with IF or SWITCH syntax
- Omitting END keyword
Employees with column DepartmentID, what will this query return?SELECT EmployeeName,
CASE DepartmentID
WHEN 1 THEN 'HR'
WHEN 2 THEN 'Sales'
ELSE 'Other'
END AS DeptName
FROM Employees;
Solution
Step 1: Understand the CASE expression in the query
The CASE checks DepartmentID: 1 returns 'HR', 2 returns 'Sales', others return 'Other'.Step 2: Analyze the SELECT output
The query returns EmployeeName and DeptName with the mapped department names or 'Other'.Final Answer:
Employee names with department names 'HR', 'Sales', or 'Other' based on DepartmentID. -> Option AQuick Check:
CASE maps DepartmentID to names [OK]
- Thinking ELSE is required (it's optional)
- Assuming only HR and Sales appear
- Expecting numeric DepartmentID instead of names
SELECT ProductName,
CASE ProductCategory
WHEN 'A' THEN 'Category A'
WHEN 'B' 'Category B'
ELSE 'Other'
END AS CategoryName
FROM Products;
Solution
Step 1: Review each WHEN clause syntax
WHEN clauses must have THEN keyword before the result. The second WHEN is missing THEN.Step 2: Check other parts of the query
END keyword is present, ELSE is correctly used, parentheses are not required around CASE expression.Final Answer:
Missing THEN keyword after WHEN 'B'. -> Option DQuick Check:
WHEN must be followed by THEN [OK]
- Forgetting THEN after WHEN
- Thinking END is optional
- Adding unnecessary parentheses
- 'Low' for 0
- 'Medium' for 1
- 'High' for 2 or more (default)?
SELECT SaleID,
CASE SaleAmount
WHEN 0 THEN 'Low'
WHEN 1 THEN 'Medium'
ELSE 'High'
END AS SaleCategory
FROM Sales;
Solution
Step 1: Understand Simple CASE limitations
Simple CASE matches exact values; ELSE handles all other cases.Step 2: Analyze the query logic
WHEN 0 returns 'Low', WHEN 1 returns 'Medium', ELSE covers 2 or more as 'High'.Final Answer:
Correctly categorizes sales as described. -> Option CQuick Check:
ELSE covers all unmatched values [OK]
- Thinking ELSE can't cover multiple values
- Trying to use ranges in Simple CASE
- Expecting WHEN for every possible value
Ready to prove it?
Pass the test and this pattern moves unknown
