0
0
SQLquery~10 mins

WHILE loops in procedures in SQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start a WHILE loop that runs as long as the counter is less than 5.

SQL
DECLARE @counter INT = 0;
WHILE [1] BEGIN
  SET @counter = @counter + 1;
END;
Drag options to blanks, or click blank then click option'
A@counter < 5
B@counter = 5
C@counter > 5
D@counter >= 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '<' in the condition.
Using '>=' which would stop the loop immediately.
2fill in blank
medium

Complete the code to increment the variable inside the WHILE loop.

SQL
DECLARE @i INT = 1;
WHILE @i <= 3 BEGIN
  SET @i = [1];
END;
Drag options to blanks, or click blank then click option'
A@i * 2
B@i - 1
C@i + 1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Decreasing the counter which causes an infinite loop.
Setting the counter to a fixed number inside the loop.
3fill in blank
hard

Fix the error in the WHILE loop condition to avoid an infinite loop.

SQL
DECLARE @num INT = 10;
WHILE [1] BEGIN
  SET @num = @num - 1;
END;
Drag options to blanks, or click blank then click option'
A@num < 10
B@num = 10
C@num >= 10
D@num > 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a condition that never becomes false, causing an infinite loop.
Using '=' which checks for equality, not a range.
4fill in blank
hard

Fill both blanks to create a WHILE loop that counts down from 5 to 1.

SQL
DECLARE @count INT = 5;
WHILE [1] BEGIN
  PRINT @count;
  SET @count = [2];
END;
Drag options to blanks, or click blank then click option'
A@count > 0
B@count + 1
C@count - 1
D@count < 5
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing the count inside the loop causing an infinite loop.
Using a wrong condition that never becomes false.
5fill in blank
hard

Fill all three blanks to create a WHILE loop that sums numbers from 1 to 5.

SQL
DECLARE @sum INT = 0;
DECLARE @num INT = 1;
WHILE [1] BEGIN
  SET @sum = @sum + [2];
  SET @num = [3];
END;
Drag options to blanks, or click blank then click option'
A@num <= 5
B@num
C@num + 1
D@sum < 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong loop condition that stops too early or never stops.
Not incrementing the number inside the loop causing an infinite loop.