Bird
0
0

What will be the output of this PL/pgSQL block?

medium📝 query result Q4 of 15
PostgreSQL - Advanced PL/pgSQL
What will be the output of this PL/pgSQL block?
DO $$
DECLARE
  counter INTEGER := 3;
BEGIN
  WHILE counter > 0 LOOP
    RAISE NOTICE 'Count: %', counter;
    counter := counter - 1;
  END LOOP;
END $$;
ACount: 3 Count: 2 Count: 1 Count: 0
BCount: 1 Count: 2 Count: 3
CCount: 3 Count: 2 Count: 1
DNo output, syntax error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the WHILE loop condition and decrement

    The loop runs while counter > 0, starting at 3 and decreasing by 1 each iteration.
  2. Step 2: Trace the output of RAISE NOTICE

    It prints 'Count: 3', then 'Count: 2', then 'Count: 1'. When counter reaches 0, loop stops.
  3. Final Answer:

    Count: 3 Count: 2 Count: 1 -> Option C
  4. Quick Check:

    Loop output = Counts 3 to 1 [OK]
Quick Trick: WHILE loops run until condition false, decrement carefully [OK]
Common Mistakes:
  • Including count 0 output which does not print
  • Reversing output order
  • Assuming syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes