CASE WHEN expression in MySQL - Time & Space Complexity
We want to understand how the time it takes to run a SQL query with a CASE WHEN expression changes as the data grows.
Specifically, we ask: How does the number of rows affect the work done by CASE WHEN?
Analyze the time complexity of the following code snippet.
SELECT
id,
name,
CASE
WHEN score >= 90 THEN 'A'
WHEN score >= 80 THEN 'B'
WHEN score >= 70 THEN 'C'
ELSE 'F'
END AS grade
FROM students;
This query assigns a grade to each student based on their score using CASE WHEN.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The CASE WHEN expression is evaluated once for each row in the students table.
- How many times: Exactly once per row, so as many times as there are rows.
As the number of rows grows, the database checks the CASE WHEN conditions for each row separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 CASE checks |
| 100 | 100 CASE checks |
| 1000 | 1000 CASE checks |
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 query grows linearly with the number of rows in the table.
[X] Wrong: "CASE WHEN runs only once regardless of rows because it's just a condition."
[OK] Correct: The CASE WHEN expression is checked for every row, so it runs as many times as there are rows.
Understanding how CASE WHEN scales helps you explain query performance clearly and shows you know how SQL processes data row by row.
"What if we added a JOIN that doubles the number of rows? How would the time complexity change?"
Practice
CASE WHEN expression do in a MySQL query?Solution
Step 1: Understand the purpose of CASE WHEN
The CASE WHEN expression checks conditions and returns values accordingly.Step 2: Compare with other options
Creating tables, deleting rows, or sorting are different SQL operations, not done by CASE WHEN.Final Answer:
It returns different values based on specified conditions. -> Option DQuick Check:
CASE WHEN = conditional value return [OK]
- Thinking CASE WHEN creates or deletes tables
- Confusing CASE WHEN with ORDER BY sorting
- Assuming CASE WHEN changes data permanently
Solution
Step 1: Check the correct CASE WHEN syntax
The correct syntax includes CASE WHEN condition THEN result ELSE result END.Step 2: Identify syntax errors in other options
The other options miss keywords or punctuation: some omit WHEN, others forget END, or misplace FROM.Final Answer:
SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END FROM students; -> Option AQuick Check:
Correct CASE WHEN syntax = SELECT CASE WHEN score > 50 THEN 'Pass' ELSE 'Fail' END FROM students; [OK]
- Omitting WHEN keyword
- Forgetting END at the end
- Misplacing FROM clause
employees with a column salary, what will this query return?SELECT name, CASE WHEN salary >= 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
Solution
Step 1: Understand the CASE WHEN usage in SELECT
The query labels each employee's salary as 'High' if >= 5000, else 'Low'.Step 2: Check what columns are selected
It selects employee names and the computed salary_level, so all employees appear.Final Answer:
A list of employee names with 'High' or 'Low' based on salary threshold 5000. -> Option CQuick Check:
CASE WHEN labels salaries, all employees listed [OK]
- Thinking CASE WHEN filters rows instead of labeling
- Assuming query returns error
- Ignoring selected columns
SELECT name, CASE salary > 10000 THEN 'Rich' ELSE 'Average' END AS status FROM customers;
Solution
Step 1: Review CASE WHEN syntax
CASE must be followed by WHEN before condition; here WHEN is missing.Step 2: Check other parts of the query
END keyword is present, alias is correct, ELSE is properly used after THEN.Final Answer:
Missing WHEN keyword after CASE. -> Option AQuick Check:
CASE requires WHEN after CASE [OK]
- Omitting WHEN keyword
- Confusing ELSE and THEN order
- Forgetting END keyword
Solution
Step 1: Analyze the price ranges needed
'Cheap' is price less than 20, 'Moderate' is price between 20 and 100 inclusive, 'Expensive' is above 100.Step 2: Check each option's conditions
CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END correctly uses price < 20 for 'Cheap', price >= 20 and <= 100 for 'Moderate', else 'Expensive'. Others have incorrect boundaries.Final Answer:
CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END -> Option BQuick Check:
Correct range checks = CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END [OK]
- Overlapping or missing ranges
- Using incorrect comparison operators
- Not covering all possible values
Ready to prove it?
Pass the test and this pattern moves unknown
