0
0
PostgreSQLquery~20 mins

LOOP, WHILE, FOR iterations in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loop Mastery in PL/pgSQL
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple FOR loop in PL/pgSQL
What will be the output of the following PL/pgSQL block?
DO $$
DECLARE
  i INT;
BEGIN
  FOR i IN 1..3 LOOP
    RAISE NOTICE 'Number: %', i;
  END LOOP;
END $$;
PostgreSQL
DO $$
DECLARE
  i INT;
BEGIN
  FOR i IN 1..3 LOOP
    RAISE NOTICE 'Number: %', i;
  END LOOP;
END $$;
ASyntax error due to missing semicolon
B
Number: 0
Number: 1
Number: 2
Number: 3
C
Number: 1
Number: 2
D
Number: 1
Number: 2
Number: 3
Attempts:
2 left
💡 Hint
The FOR loop runs from 1 to 3 inclusive.
query_result
intermediate
2:00remaining
WHILE loop behavior with decrement
What will be the output of this PL/pgSQL block?
DO $$
DECLARE
  counter INT := 3;
BEGIN
  WHILE counter > 0 LOOP
    RAISE NOTICE 'Count: %', counter;
    counter := counter - 1;
  END LOOP;
END $$;
PostgreSQL
DO $$
DECLARE
  counter INT := 3;
BEGIN
  WHILE counter > 0 LOOP
    RAISE NOTICE 'Count: %', counter;
    counter := counter - 1;
  END LOOP;
END $$;
A
Count: 3
Count: 2
Count: 1
B
Count: 3
Count: 2
Count: 1
Count: 0
C
Count: 2
Count: 1
Count: 0
DInfinite loop error
Attempts:
2 left
💡 Hint
The loop stops when counter is no longer greater than 0.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this FOR loop
Which option contains the correct syntax for a FOR loop iterating over integers 1 to 5 in PL/pgSQL?
A
FOR i IN 1 TO 5 LOOP
  RAISE NOTICE '%', i;
END LOOP;
B
FOR i IN 1..5 LOOP
  RAISE NOTICE '%', i;
END LOOP;
C
FOR i FROM 1 TO 5 LOOP
  RAISE NOTICE '%', i;
END LOOP;
D
FOR i IN RANGE(1,5) LOOP
  RAISE NOTICE '%', i;
END LOOP;
Attempts:
2 left
💡 Hint
PL/pgSQL uses two dots .. for ranges.
query_result
advanced
2:00remaining
Output of nested loops with conditional exit
What will be the output of this PL/pgSQL block?
DO $$
DECLARE
  i INT;
  j INT;
BEGIN
  FOR i IN 1..2 LOOP
    FOR j IN 1..3 LOOP
      IF i * j > 3 THEN
        EXIT;
      END IF;
      RAISE NOTICE 'i=% j=%', i, j;
    END LOOP;
  END LOOP;
END $$;
PostgreSQL
DO $$
DECLARE
  i INT;
  j INT;
BEGIN
  FOR i IN 1..2 LOOP
    FOR j IN 1..3 LOOP
      IF i * j > 3 THEN
        EXIT;
      END IF;
      RAISE NOTICE 'i=% j=%', i, j;
    END LOOP;
  END LOOP;
END $$;
A
i=1 j=1
i=1 j=2
i=1 j=3
i=2 j=1
B
i=1 j=1
i=1 j=2
i=1 j=3
i=2 j=1
i=2 j=2
C
i=1 j=1
i=1 j=2
i=2 j=1
i=2 j=2
D
i=1 j=1
i=1 j=2
i=2 j=1
i=2 j=2
i=2 j=3
Attempts:
2 left
💡 Hint
The inner loop exits when i*j > 3, skipping further j values.
🧠 Conceptual
expert
2:00remaining
Effect of EXIT vs CONTINUE in loops
In PL/pgSQL loops, what is the difference between EXIT and CONTINUE statements?
AEXIT skips the current iteration; CONTINUE stops the loop completely.
BEXIT and CONTINUE both stop the loop but differ in syntax.
CEXIT stops the current loop entirely; CONTINUE skips to the next iteration of the loop.
DEXIT restarts the loop from the beginning; CONTINUE ends the loop.
Attempts:
2 left
💡 Hint
Think about how to stop a loop vs skip one iteration.