CASE in PL/pgSQL in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run a CASE statement in PL/pgSQL changes as the number of conditions grows.
How does adding more conditions affect the work the database does?
Analyze the time complexity of the following PL/pgSQL CASE statement.
DECLARE
grade CHAR := 'B';
result TEXT;
BEGIN
CASE grade
WHEN 'A' THEN result := 'Excellent';
WHEN 'B' THEN result := 'Good';
WHEN 'C' THEN result := 'Average';
WHEN 'D' THEN result := 'Below Average';
ELSE result := 'Fail';
END CASE;
END;
This code checks the value of grade and sets result based on matching conditions.
Look for repeated checks or comparisons inside the CASE.
- Primary operation: Comparing the input value to each WHEN condition.
- How many times: Up to the number of WHEN conditions, until a match is found.
As the number of WHEN conditions increases, the number of comparisons can grow.
| Input Size (number of WHENs) | Approx. Comparisons |
|---|---|
| 5 | Up to 5 |
| 50 | Up to 50 |
| 500 | Up to 500 |
Pattern observation: The number of comparisons grows roughly in a straight line with the number of conditions.
Time Complexity: O(n)
This means the time to find a match grows linearly with the number of CASE conditions.
[X] Wrong: "The CASE statement always runs in constant time no matter how many conditions there are."
[OK] Correct: The database checks conditions one by one until it finds a match, so more conditions mean more checks and more time.
Understanding how CASE statements scale helps you write efficient conditional logic in database functions, a useful skill in many real projects.
"What if we replaced the CASE with a lookup table join? How would the time complexity change?"
Practice
CASE in PL/pgSQL?Solution
Step 1: Understand the role of CASE
CASE is used to select one action from many based on conditions, like a traffic light deciding when to stop or go.Step 2: Compare with other options
Loops repeat actions, table creation defines structure, and variables store data, none of which is the main role of CASE.Final Answer:
To choose different actions based on conditions -> Option AQuick Check:
CASE chooses actions based on conditions [OK]
- Confusing CASE with loops
- Thinking CASE creates tables
- Assuming CASE stores data permanently
Solution
Step 1: Recall PL/pgSQL syntax for CASE
In PL/pgSQL, a CASE block must be closed withEND CASE;to mark its end clearly.Step 2: Check other options
END;ends blocks like functions, but CASE specifically needsEND CASE;.FINISH CASE;andSTOP;are invalid keywords.Final Answer:
END CASE; -> Option DQuick Check:
CASE ends with END CASE; [OK]
- Using END; alone to close CASE
- Writing FINISH CASE; which is invalid
- Using STOP; which is not a PL/pgSQL keyword
DECLARE
grade CHAR := 'B';
result TEXT;
BEGIN
CASE grade
WHEN 'A' THEN result := 'Excellent';
WHEN 'B' THEN result := 'Good';
ELSE result := 'Average';
END CASE;
RETURN result;
END;What will be the returned value?
Solution
Step 1: Identify the value of grade
The variablegradeis set to 'B'.Step 2: Match grade in CASE
CASE checks 'B', matches the second WHEN clause, soresultbecomes 'Good'.Final Answer:
'Good' -> Option AQuick Check:
grade 'B' returns 'Good' [OK]
- Choosing ELSE when a WHEN matches
- Confusing variable assignment inside CASE
- Assuming NULL if no ELSE present
DECLARE
score INT := 85;
grade TEXT;
BEGIN
CASE
WHEN score >= 90 THEN grade := 'A';
WHEN score >= 80 THEN grade := 'B';
ELSE grade := 'C';
END;
RETURN grade;
END;Solution
Step 1: Check CASE block ending
The CASE block is closed withEND;but PL/pgSQL requiresEND CASE;to close CASE.Step 2: Verify other parts
Variable declarations and comparisons are correct, and ELSE clause is present.Final Answer:
Missing END CASE; to close CASE block -> Option CQuick Check:
CASE must end with END CASE; [OK]
- Using END; instead of END CASE;
- Thinking ELSE is optional here
- Misreading comparison operators
Solution
Step 1: Handle NULL explicitly
Since NULL cannot be matched by simple WHEN, useWHEN score IS NULLto check NULL values.Step 2: Order conditions correctly
Check NULL first, then score >= 50 for 'Pass', else 'Fail'. This matchesCASE WHEN score IS NULL THEN RETURN 'Invalid'; WHEN score >= 50 THEN RETURN 'Pass'; ELSE RETURN 'Fail'; END CASE;
.Final Answer:
CASE with WHEN score IS NULL, then score >= 50, else Fail -> Option BQuick Check:
Use IS NULL to check NULL in CASE [OK]
- Trying to match NULL with WHEN NULL
- Using CASE score with conditions inside WHEN
- Not checking NULL before other conditions
