IF-ELSE in procedures in SQL - Time & Space Complexity
When we use IF-ELSE in SQL procedures, we want to know how the time to run the code changes as the input grows.
We ask: Does the IF-ELSE make the procedure slower when data gets bigger?
Analyze the time complexity of the following procedure using IF-ELSE.
CREATE PROCEDURE CheckValue(IN input_val INT)
BEGIN
IF input_val < 100 THEN
SELECT 'Small value';
ELSE
SELECT 'Large value';
END IF;
END;
This procedure checks if a number is less than 100 and returns a message accordingly.
Look for loops or repeated steps inside the procedure.
- Primary operation: A single IF-ELSE decision.
- How many times: The decision runs once per procedure call.
The procedure does one check no matter the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 check |
| 100 | 1 check |
| 1000 | 1 check |
Pattern observation: The work stays the same even if the input number is bigger.
Time Complexity: O(1)
This means the procedure runs in constant time, doing the same amount of work no matter the input.
[X] Wrong: "IF-ELSE makes the procedure slower as input grows because it checks conditions repeatedly."
[OK] Correct: The IF-ELSE runs only once per call, so input size does not affect how many times it runs.
Understanding that simple IF-ELSE decisions do not add time as data grows helps you explain procedure efficiency clearly and confidently.
"What if the procedure had a loop inside the ELSE part that runs based on input size? How would the time complexity change?"