Bird
0
0

In PL/pgSQL, which syntax correctly uses FOREACH to loop through all elements of an integer array numbers?

easy📝 Syntax Q3 of 15
PostgreSQL - PL/pgSQL Fundamentals
In PL/pgSQL, which syntax correctly uses FOREACH to loop through all elements of an integer array numbers?
AFOREACH num LOOP ... END LOOP;
BFOREACH num IN numbers LOOP ... END LOOP;
CFOREACH num IN ARRAY numbers LOOP ... END LOOP;
DFOREACH num IN ARRAY(numbers) LOOP ... END LOOP;
Step-by-Step Solution
Solution:
  1. Step 1: Understand FOREACH syntax

    FOREACH requires the keyword ARRAY before the array variable to iterate over its elements.
  2. Step 2: Analyze options

    FOREACH num IN ARRAY numbers LOOP ... END LOOP; uses 'FOREACH num IN ARRAY numbers LOOP', which is the correct syntax.
    FOREACH num IN numbers LOOP ... END LOOP; misses ARRAY keyword.
    FOREACH num LOOP ... END LOOP; lacks the array reference.
    FOREACH num IN ARRAY(numbers) LOOP ... END LOOP; uses parentheses incorrectly.
  3. Final Answer:

    FOREACH num IN ARRAY numbers LOOP ... END LOOP; -> Option C
  4. Quick Check:

    FOREACH must include 'IN ARRAY' before the array variable [OK]
Quick Trick: Always use 'IN ARRAY' to iterate arrays with FOREACH [OK]
Common Mistakes:
  • Omitting the ARRAY keyword
  • Using parentheses around the array variable
  • Not specifying the loop variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes