CASE expression in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to run a CASE expression changes as the data grows.
How does the number of rows affect the work done by the CASE expression?
Analyze the time complexity of the following code snippet.
SELECT id,
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 a CASE expression.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The CASE expression is evaluated once for each row in the students table.
- How many times: Exactly as many times as there are rows (students) in the table.
Each student row requires checking the CASE conditions once.
| 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 expression grows linearly with the number of rows.
[X] Wrong: "CASE expressions run in constant time no matter how many rows there are."
[OK] Correct: The CASE expression runs once per row, so more rows mean more evaluations and more time.
Understanding how CASE expressions scale helps you explain query performance clearly and shows you know how databases handle row-by-row logic.
"What if the CASE expression had nested CASE statements inside? How would that affect the time complexity?"
Practice
CASE expression do in PostgreSQL?Solution
Step 1: Understand the purpose of CASE
The CASE expression evaluates conditions and returns a result based on which condition is true.Step 2: Compare with other SQL commands
Unlike CREATE, DELETE, or UPDATE, CASE does not change data but returns values conditionally.Final Answer:
Returns different results based on conditions -> Option AQuick Check:
CASE = conditional results [OK]
- Thinking CASE modifies data
- Confusing CASE with table operations
- Ignoring ELSE for default results
Solution
Step 1: Recall correct CASE syntax
The correct syntax starts with CASE, followed by WHEN conditions, THEN results, optional ELSE, and ends with END.Step 2: Identify syntax errors in options
The syntax with 'CASE IF ... END' uses 'IF' instead of 'WHEN'. Syntaxes omitting 'END' (such as 'CASE WHEN ... default' or 'CASE condition ...') are invalid. Only the full syntax is correct.Final Answer:
CASE WHEN condition THEN result ELSE default END -> Option BQuick Check:
CASE syntax requires WHEN and END [OK]
- Using IF instead of WHEN
- Forgetting END at the end
- Omitting ELSE clause
employees with column salary, what will this query return?SELECT salary, CASE WHEN salary > 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
Solution
Step 1: Understand the CASE condition
The CASE checks if salary is greater than 5000; if true, returns 'High', else 'Low'.Step 2: Analyze the SELECT output
The query returns all salaries with a new column showing 'High' or 'Low' labels accordingly.Final Answer:
A list of salaries with labels 'High' or 'Low' based on salary value -> Option AQuick Check:
CASE labels salaries as High or Low [OK]
- Thinking CASE filters rows
- Assuming ELSE is mandatory (it is here but present)
- Confusing output with WHERE clause filtering
SELECT name, CASE WHEN age > 18 THEN 'Adult' WHEN age < 18 THEN 'Minor' END AS age_group FROM users;
Solution
Step 1: Check CASE conditions coverage
The CASE covers age > 18 and age < 18 but misses age = 18 case.Step 2: Understand ELSE role
Without ELSE, age = 18 returns NULL. Adding ELSE handles this missing case.Final Answer:
Missing ELSE clause for age = 18 -> Option DQuick Check:
CASE needs ELSE for unmatched conditions [OK]
- Assuming CASE covers all values without ELSE
- Confusing END keyword placement
- Misusing THEN keyword
Solution
Step 1: Analyze price ranges for categories
'Cheap' is price less than 20, 'Moderate' is price from 20 up to but not including 100, 'Expensive' is 100 or more.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 < 20, then >= 20 and < 100, else 'Expensive'. CASE WHEN price < 20 THEN 'Cheap' WHEN price > 20 AND price < 100 THEN 'Moderate' ELSE 'Expensive' END misses price = 20 in 'Moderate'. CASE WHEN price <= 20 THEN 'Cheap' WHEN price >= 20 AND price < 100 THEN 'Moderate' ELSE 'Expensive' END overlaps at 20 in both 'Cheap' and 'Moderate'. CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price <= 100 THEN 'Moderate' ELSE 'Expensive' END includes 100 in 'Moderate' but should be 'Expensive'.Final Answer:
CASE WHEN price < 20 THEN 'Cheap' WHEN price >= 20 AND price < 100 THEN 'Moderate' ELSE 'Expensive' END -> Option CQuick Check:
Use inclusive and exclusive ranges carefully in CASE [OK]
- Overlapping or missing ranges
- Using > instead of >= causing gaps
- Misplacing ELSE category boundaries
Ready to prove it?
Pass the test and this pattern moves unknown
