WHILE loops in procedures in SQL - Time & Space Complexity
When using WHILE loops in SQL procedures, it is important to understand how the number of steps grows as the loop runs more times.
We want to know how the total work changes when the loop runs many times.
Analyze the time complexity of the following SQL procedure using a WHILE loop.
CREATE PROCEDURE CountToN(@n INT)
AS
BEGIN
DECLARE @i INT = 1;
WHILE @i <= @n
BEGIN
-- Some simple operation
SET @i = @i + 1;
END
END
This procedure counts from 1 up to a number n, doing a simple step each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The WHILE loop runs a simple step repeatedly.
- How many times: It runs once for each number from 1 up to n.
As n gets bigger, the number of loop steps grows directly with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The work grows evenly as n grows; doubling n doubles the steps.
Time Complexity: O(n)
This means the total work grows in a straight line with the size of n.
[X] Wrong: "The loop runs in constant time no matter how big n is."
[OK] Correct: Each loop step happens once per number up to n, so more n means more steps.
Understanding how loops affect time helps you explain how your code scales and shows you can think about efficiency clearly.
"What if we added a nested WHILE loop inside the first one that also runs n times? How would the time complexity change?"
Practice
WHILE loop inside a SQL procedure?Solution
Step 1: Understand the role of WHILE loops
AWHILEloop runs repeatedly as long as its condition remains true.Step 2: Identify the purpose in procedures
Inside procedures,WHILEloops automate repeated tasks by running code multiple times.Final Answer:
To repeat a set of SQL statements while a condition is true -> Option AQuick Check:
WHILE loops repeat code while condition true = D [OK]
- Confusing WHILE with creating or deleting tables
- Thinking WHILE joins tables
- Assuming WHILE runs only once
Solution
Step 1: Recall standard SQL procedure syntax
In many SQL dialects, aWHILEloop starts withWHILE condition BEGINto mark the loop start.Step 2: Compare options
WHILE condition BEGIN matches the correct syntax without extra parentheses or keywords like DO or LOOP.Final Answer:
WHILE condition BEGIN -> Option BQuick Check:
WHILE condition BEGIN is correct syntax [OK]
- Adding DO after WHILE (not standard in SQL)
- Using LOOP keyword (not in standard SQL)
- Putting condition in parentheses unnecessarily
DECLARE @counter INT = 1;
WHILE @counter <= 3
BEGIN
PRINT @counter;
SET @counter = @counter + 1;
END
What will be printed when this runs?
Solution
Step 1: Analyze loop initialization and condition
The counter starts at 1 and loops while it is less than or equal to 3.Step 2: Trace loop iterations
It prints 1, then increments to 2, prints 2, increments to 3, prints 3, then increments to 4 and stops.Final Answer:
1 2 3 -> Option AQuick Check:
Loop prints 1 to 3 inclusive = A [OK]
- Thinking loop prints 4 because of last increment
- Confusing starting value as 0
- Assuming syntax error due to PRINT
DECLARE @i INT = 1;
WHILE @i < 5
BEGIN
PRINT @i;
END
Solution
Step 1: Check loop body for variable update
The variable @i is never changed inside the loop, so condition stays true forever.Step 2: Understand effect of missing increment
Without increment, loop runs infinitely printing @i repeatedly.Final Answer:
Missing increment of @i inside the loop causing infinite loop -> Option CQuick Check:
Loop variable must update to avoid infinite loop = B [OK]
- Thinking PRINT syntax is wrong
- Believing declaration is incorrect
- Assuming condition operator causes error
Solution
Step 1: Check loop condition and variable updates
DECLARE @sum INT = 0, @i INT = 1;
WHILE @i <= @N
BEGIN
SET @sum = @sum + @i;
SET @i = @i + 1;
END correctly loops from 1 to N, adding each number to sum and incrementing i.Step 2: Identify errors in other options
DECLARE @sum INT = 0, @i INT = 1;
WHILE @i < @N
BEGIN
SET @sum = @sum + @i;
END misses increment of i, causing infinite loop. DECLARE @sum INT = 0, @i INT = 1;
WHILE @i <= @N
BEGIN
SET @sum = @sum + @i;
SET @i = @i - 1;
END decrements i, causing infinite loop. DECLARE @sum INT = 0;
WHILE @sum <= @N
BEGIN
SET @sum = @sum + 1;
END incorrectly uses sum as loop variable.Final Answer:
DECLARE @sum INT = 0, @i INT = 1; WHILE @i <= @N BEGIN SET @sum = @sum + @i; SET @i = @i + 1; END -> Option DQuick Check:
Increment loop variable and add to sum = A [OK]
- Forgetting to increment loop variable
- Decrementing loop variable causing infinite loop
- Using sum as loop condition variable
Ready to prove it?
Pass the test and this pattern moves unknown
