Why CASE expressions are needed in SQL - Performance Analysis
We want to understand how the time it takes to run a SQL query changes when we use CASE expressions.
Specifically, we ask: How does adding CASE affect the work the database does?
Analyze the time complexity of the following SQL snippet using CASE.
SELECT
employee_id,
salary,
CASE
WHEN salary > 70000 THEN 'High'
WHEN salary > 40000 THEN 'Medium'
ELSE 'Low'
END AS salary_level
FROM employees;
This query classifies each employee's salary into categories using CASE.
Look for repeated work done by the database.
- Primary operation: Checking each employee's salary against conditions in CASE.
- How many times: Once for every employee row in the table.
As the number of employees grows, the database checks more salaries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 salary checks |
| 100 | About 100 salary checks |
| 1000 | About 1000 salary checks |
Pattern observation: The work grows directly with the number of employees.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows grows.
[X] Wrong: "CASE expressions make queries much slower because they add many checks."
[OK] Correct: Each row is checked once, so the extra work is small and grows linearly, not exponentially.
Understanding how CASE expressions affect query time helps you explain your SQL choices clearly and confidently.
"What if we added nested CASE expressions inside the main CASE? How would the time complexity change?"
Practice
CASE expressions in SQL queries?Solution
Step 1: Understand the purpose of CASE expressions
CASE expressions allow SQL queries to return different results depending on conditions you specify.Step 2: Compare with other SQL operations
Creating tables, deleting rows, or changing schema are done by other SQL commands, not CASE.Final Answer:
To return different values based on conditions within a query -> Option DQuick Check:
CASE = conditional output [OK]
- Thinking CASE creates or deletes tables
- Confusing CASE with schema changes
- Assuming CASE modifies data directly
Solution
Step 1: Recall the standard CASE syntax
The correct syntax starts with CASE, followed by WHEN condition THEN result, optional ELSE, and ends with END.Step 2: Identify incorrect options
IF and SWITCH are not valid SQL CASE keywords; SELECT CASE is incomplete syntax.Final Answer:
CASE WHEN condition THEN result ELSE other END -> Option BQuick Check:
CASE syntax = WHEN...THEN...END [OK]
- Using IF instead of CASE in SQL
- Omitting END keyword
- Starting CASE with SELECT
Employees with column Salary, what will this query return?SELECT Salary, CASE WHEN Salary > 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;
Solution
Step 1: Understand the CASE logic in SELECT
The CASE checks each Salary; if greater than 5000, label 'High', else 'Low'.Step 2: Check what rows are returned
The query returns all rows with Salary and corresponding label, no rows are filtered out.Final Answer:
A list of salaries with labels 'High' for salaries above 5000 and 'Low' otherwise -> Option CQuick Check:
CASE labels salaries conditionally [OK]
- Thinking CASE filters rows like WHERE
- Assuming only 'High' salaries show
- Believing CASE causes syntax error in SELECT
SELECT Name, CASE Salary > 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;
Solution
Step 1: Review CASE syntax
CASE must be followed by WHEN condition THEN result; here WHEN is missing.Step 2: Check other parts
END keyword is present; alias usage is correct; CASE is valid in SELECT.Final Answer:
Missing WHEN keyword after CASE -> Option AQuick Check:
CASE requires WHEN after CASE [OK]
- Omitting WHEN keyword
- Confusing END with alias
- Thinking CASE can't be in SELECT
Score in a table:- Score ≥ 90: 'A'
- Score ≥ 80 and < 90: 'B'
- Score ≥ 70 and < 80: 'C'
- Otherwise: 'F'
Which CASE expression correctly implements this grading logic?
Solution
Step 1: Understand the grading order
Grades must be checked from highest to lowest score to assign correct grade.Step 2: Analyze each option
CASE WHEN Score ≥ 90 THEN 'A' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 70 THEN 'C' ELSE 'F' END checks from highest to lowest correctly; CASE WHEN Score < 70 THEN 'F' WHEN Score < 90 THEN 'C' WHEN Score < 80 THEN 'B' ELSE 'A' END incorrectly assigns scores 70-89 to 'C'; CASE WHEN Score ≥ 70 THEN 'C' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 90 THEN 'A' ELSE 'F' END checks in wrong order; CASE Score WHEN ≥ 90 THEN 'A' WHEN ≥ 80 THEN 'B' WHEN ≥ 70 THEN 'C' ELSE 'F' END uses invalid CASE syntax.Final Answer:
CASE WHEN Score ≥ 90 THEN 'A' WHEN Score ≥ 80 THEN 'B' WHEN Score ≥ 70 THEN 'C' ELSE 'F' END -> Option AQuick Check:
Order conditions high to low in CASE [OK]
- Checking conditions in wrong order
- Using CASE with invalid syntax
- Assuming CASE compares values directly without WHEN
Ready to prove it?
Pass the test and this pattern moves unknown
