Bird
0
0

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

medium📝 query result Q4 of 15
PostgreSQL - PL/pgSQL Fundamentals
What will be the output of this PL/pgSQL block?
DECLARE
  arr integer[] := ARRAY[1, 2, 3];
  result text := '';
BEGIN
  FOREACH val IN ARRAY arr LOOP
    result := result || val || ',';
  END LOOP;
  RAISE NOTICE '%', result;
END;
A1,2,3,
B123
C1;2;3;
DError due to syntax
Step-by-Step Solution
Solution:
  1. Step 1: Understand the loop operation

    The FOREACH loops over each integer in arr: 1, 2, 3.
  2. Step 2: Check string concatenation

    Each val is appended to result with a comma, so result becomes '1,2,3,'.
  3. Final Answer:

    1,2,3, -> Option A
  4. Quick Check:

    FOREACH concatenates values with commas [OK]
Quick Trick: FOREACH appends each element in order [OK]
Common Mistakes:
  • Expecting no commas in output
  • Thinking output is '123' without separators
  • Assuming syntax error in simple loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes