0
0
SQLquery~20 mins

WHILE loops in procedures in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WHILE Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple WHILE loop procedure
Consider the following SQL procedure that uses a WHILE loop to insert numbers into a table. What will be the content of the table Numbers after executing the procedure?
SQL
CREATE TABLE Numbers (num INT);

CREATE PROCEDURE InsertNumbers()
BEGIN
  DECLARE i INT DEFAULT 1;
  WHILE i <= 3 DO
    INSERT INTO Numbers VALUES (i);
    SET i = i + 1;
  END WHILE;
END;

CALL InsertNumbers();
SELECT * FROM Numbers ORDER BY num;
A
1
2
3
B
1
2
C
0
1
2
3
DEmpty table
Attempts:
2 left
💡 Hint
Think about how the WHILE loop increments and when it stops.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this WHILE loop procedure
Which option contains the correct syntax for a WHILE loop inside a SQL procedure?
SQL
CREATE PROCEDURE CountDown()
BEGIN
  DECLARE x INT DEFAULT 5;
  WHILE x > 0 DO
    SET x = x - 1;
  END WHILE;
END;
AMissing DO keyword after WHILE condition
BSET statement cannot be inside WHILE loop
CDECLARE statement must be after BEGIN but before WHILE
DMissing semicolon after END WHILE
Attempts:
2 left
💡 Hint
Check punctuation carefully after loop blocks.
optimization
advanced
2:00remaining
Optimizing a WHILE loop to avoid unnecessary iterations
Given a procedure that sums numbers from 1 to N using a WHILE loop, which option optimizes the loop to reduce the number of iterations?
SQL
CREATE PROCEDURE SumNumbers(IN N INT, OUT total INT)
BEGIN
  DECLARE i INT DEFAULT 1;
  SET total = 0;
  WHILE i <= N DO
    SET total = total + i;
    SET i = i + 1;
  END WHILE;
END;
AAdd a BREAK statement inside WHILE when i > N
BChange WHILE to REPEAT loop with same logic
CReplace the WHILE loop with SET total = N*(N+1)/2;
DUse a cursor to iterate numbers instead of WHILE
Attempts:
2 left
💡 Hint
Think about mathematical formulas that can replace loops.
🔧 Debug
advanced
2:00remaining
Debugging an infinite loop in a WHILE procedure
This procedure causes an infinite loop. What is the cause?
SQL
CREATE PROCEDURE InfiniteLoop()
BEGIN
  DECLARE counter INT DEFAULT 1;
  WHILE counter <= 5 DO
    -- Missing increment
  END WHILE;
END;
AMissing BEGIN and END inside WHILE loop
BThe counter variable is never incremented inside the loop
CThe WHILE condition is incorrect syntax
DDECLARE statement is inside the loop causing reset
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
🧠 Conceptual
expert
2:00remaining
Understanding variable scope in WHILE loops inside procedures
In a SQL procedure, what happens to a variable declared inside a WHILE loop block compared to one declared before the loop?
AVariables declared inside the loop are re-declared each iteration and lose previous values
BVariables declared inside the loop keep their values between iterations
CVariables cannot be declared inside WHILE loops in SQL procedures
DVariables declared before the loop are local to the loop only
Attempts:
2 left
💡 Hint
Think about how SQL handles variable declarations inside blocks.