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?"
Practice
IF-ELSE statements inside SQL procedures?Solution
Step 1: Understand the role of IF-ELSE
IF-ELSE statements allow conditional execution of code depending on whether a condition is true or false.Step 2: Identify the correct purpose
Creating tables or deleting data are not related to IF-ELSE logic but to other SQL commands.Final Answer:
To execute different code blocks based on conditions -> Option BQuick Check:
IF-ELSE controls flow [OK]
- Thinking IF-ELSE creates or deletes tables
- Confusing IF-ELSE with data manipulation commands
- Assuming IF-ELSE changes database structure
Solution
Step 1: Recall SQL procedure IF syntax
In SQL procedures, the IF statement starts withIF condition THEN.Step 2: Compare options
Options B, C, and D use syntax styles from other languages or incorrect keywords.Final Answer:
IF condition THEN -> Option AQuick Check:
SQL IF starts with IF condition THEN [OK]
- Using curly braces {} like in programming languages
- Using DO or BEGIN instead of THEN
- Missing THEN keyword after condition
DECLARE v_score INT DEFAULT 75; IF v_score >= 90 THEN SELECT 'Grade A'; ELSEIF v_score >= 80 THEN SELECT 'Grade B'; ELSEIF v_score >= 70 THEN SELECT 'Grade C'; ELSE SELECT 'Grade F'; END IF;
What will be the output?
Solution
Step 1: Check the value of v_score
v_score is 75.Step 2: Evaluate conditions in order
75 is not >= 90, not >= 80, but it is >= 70, so 'Grade C' is selected.Final Answer:
'Grade C' -> Option CQuick Check:
75 >= 70 = 'Grade C' [OK]
- Choosing 'Grade B' or 'Grade A' without checking all conditions
- Ignoring ELSEIF order
- Assuming ELSE runs when a previous condition matches
IF score > 50 SELECT 'Pass'; ELSE SELECT 'Fail'; END IF;
Solution
Step 1: Check IF statement syntax
In SQL procedures, IF must be followed by THEN after the condition.Step 2: Verify given code
The code hasIF score > 50but no THEN keyword.Final Answer:
Missing THEN after IF condition -> Option DQuick Check:
IF condition THEN is required [OK]
- Omitting THEN after IF
- Adding semicolon after ELSE incorrectly
- Misusing END IF syntax
Solution
Step 1: Check condition order for correctness
Negative age must be checked first to return 'Invalid'. Then check if age is 18 or more for 'Adult'. Otherwise, 'Minor'.Step 2: Analyze options
IF age < 0 THEN SELECT 'Invalid'; ELSEIF age >= 18 THEN SELECT 'Adult'; ELSE SELECT 'Minor'; END IF; checks negative first, then adult, then minor. IF age >= 18 THEN SELECT 'Adult'; ELSEIF age < 0 THEN SELECT 'Invalid'; ELSE SELECT 'Minor'; END IF; checks adult before negative, which is wrong. IF age < 0 THEN SELECT 'Invalid'; ELSE SELECT 'Minor'; ELSEIF age >= 18 THEN SELECT 'Adult'; END IF; has ELSE before ELSEIF, invalid syntax. IF age >= 18 THEN SELECT 'Adult'; ELSE SELECT 'Minor'; END IF; IF age < 0 THEN SELECT 'Invalid'; END IF; splits checks into two IFs, which can cause multiple outputs.Final Answer:
IF age < 0 THEN SELECT 'Invalid'; ELSEIF age >= 18 THEN SELECT 'Adult'; ELSE SELECT 'Minor'; END IF; -> Option AQuick Check:
Check invalid first, then adult, else minor [OK]
- Checking valid age before invalid
- Using ELSE before ELSEIF
- Splitting conditions into separate IFs causing multiple outputs
Ready to prove it?
Pass the test and this pattern moves unknown
