Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the FOREACH statement do in PostgreSQL?
FOREACH loops through each element in an array, allowing you to perform actions on each item one by one.
Click to reveal answer
beginner
How do you declare a FOREACH loop to iterate over an integer array in PostgreSQL?
Use: FOREACH element_variable IN ARRAY array_variable LOOP ... END LOOP;
Click to reveal answer
intermediate
Can FOREACH be used to iterate over arrays of types other than integers?
Yes, FOREACH works with arrays of any data type, like text[], integer[], or custom types.
Click to reveal answer
beginner
What happens if the array used in FOREACH is empty?
The loop body does not execute at all because there are no elements to iterate over.
Click to reveal answer
intermediate
Why is FOREACH useful compared to a traditional FOR loop in PostgreSQL?
FOREACH simplifies looping over arrays directly without needing to manage index counters manually.
Click to reveal answer
What keyword starts the loop to iterate over an array in PostgreSQL?
AWHILE
BFOR
CFOREACH
DLOOP
✗ Incorrect
FOREACH is the specific keyword used to loop over each element in an array.
Which of these is the correct syntax to iterate over an array named my_array?
AFOREACH item IN ARRAY my_array LOOP ... END LOOP;
BFOREACH item IN my_array LOOP ... END LOOP;
CFOR item IN ARRAY my_array LOOP ... END LOOP;
DFOR item IN my_array LOOP ... END LOOP;
✗ Incorrect
FOREACH requires the IN ARRAY clause to specify the array to iterate over.
What type of variable should 'item' be in FOREACH item IN ARRAY my_array?
ASame type as elements in my_array
BAny type
CAlways integer
DAlways text
✗ Incorrect
The loop variable must match the data type of the array elements.
If my_array is empty, what happens inside the FOREACH loop?
ALoop runs infinitely
BLoop runs once with NULL
CError occurs
DLoop runs zero times
✗ Incorrect
No elements means the loop body does not execute.
FOREACH is best used when you want to:
ALoop a fixed number of times
BProcess each element of an array easily
CLoop through table rows
DCreate arrays
✗ Incorrect
FOREACH is designed to iterate over array elements simply.
Explain how to use FOREACH to loop through an array in PostgreSQL.
Think about the syntax and what each part means.
You got /4 concepts.
Describe a real-life scenario where FOREACH for array iteration would be helpful in a database function.
Imagine you have a list of things to handle one by one.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the FOREACH statement in PostgreSQL when working with arrays?
easy
A. To convert an array into a string.
B. To loop through each element of an array easily inside PL/pgSQL.
C. To sort the elements of an array in ascending order.
D. To create a new array from existing tables.
Solution
Step 1: Understand FOREACH usage context
FOREACH is used inside PL/pgSQL to iterate over array elements one by one.
Step 2: Identify the main purpose
It simplifies looping through arrays without manual index handling.
Final Answer:
To loop through each element of an array easily inside PL/pgSQL. -> Option B
Quick Check:
FOREACH loops over array elements [OK]
Hint: FOREACH loops over array elements inside PL/pgSQL [OK]
Common Mistakes:
Confusing FOREACH with array creation or sorting
Thinking FOREACH works outside PL/pgSQL
Assuming FOREACH converts arrays to strings
2. Which of the following is the correct syntax to iterate over an integer array arr using FOREACH in PL/pgSQL?
easy
A. FOREACH element IN ARRAY arr LOOP ... END LOOP;
B. FOREACH element FROM arr LOOP ... END LOOP;
C. FOREACH element ON arr LOOP ... END LOOP;
D. FOREACH element OVER arr LOOP ... END LOOP;
Solution
Step 1: Recall FOREACH syntax
The correct syntax uses 'IN ARRAY' to specify the array to loop over.
Step 2: Match syntax options
Only FOREACH element IN ARRAY arr LOOP ... END LOOP; uses 'IN ARRAY' correctly; others use invalid keywords.
Final Answer:
FOREACH element IN ARRAY arr LOOP ... END LOOP; -> Option A
Quick Check:
FOREACH ... IN ARRAY ... is correct syntax [OK]
Hint: Use 'IN ARRAY' to loop over arrays with FOREACH [OK]
Common Mistakes:
Using FROM, ON, or OVER instead of IN ARRAY
Omitting LOOP or END LOOP keywords
Trying to use FOREACH outside PL/pgSQL
3. Consider the following PL/pgSQL block:
DECLARE
arr integer[] := ARRAY[2, 4, 6];
sum integer := 0;
BEGIN
FOREACH val IN ARRAY arr LOOP
sum := sum + val;
END LOOP;
RAISE NOTICE '%', sum;
END;
What will be the output when this block runs?
medium
A. Syntax error
B. 24
C. 12
D. 0
Solution
Step 1: Understand the loop iteration
The FOREACH loops over arr elements: 2, 4, and 6.
Step 2: Calculate the sum
sum starts at 0, then adds 2 + 4 + 6 = 12.
Final Answer:
12 -> Option C
Quick Check:
2 + 4 + 6 = 12 [OK]
Hint: Sum array elements by adding each in FOREACH loop [OK]
Common Mistakes:
Assuming FOREACH does not add elements
Confusing sum initialization
Expecting syntax error due to RAISE NOTICE
4. Identify the error in this PL/pgSQL snippet:
DECLARE
arr text[] := ARRAY['a', 'b', 'c'];
ch text;
BEGIN
FOREACH ch IN arr LOOP
RAISE NOTICE '%', ch;
END LOOP;
END;
medium
A. Variable 'ch' must be declared as integer, not text.
B. Array declaration syntax is incorrect.
C. RAISE NOTICE cannot print variables inside loops.
D. FOREACH must use 'IN ARRAY' before the array variable.
Solution
Step 1: Check FOREACH syntax
FOREACH requires 'IN ARRAY' before the array variable to iterate properly.
Step 2: Identify the error
The code uses 'IN arr' instead of 'IN ARRAY arr', causing syntax error.
Final Answer:
FOREACH must use 'IN ARRAY' before the array variable. -> Option D
Quick Check:
FOREACH ... IN ARRAY ... is correct syntax [OK]
Hint: Always write 'FOREACH var IN ARRAY arr' [OK]
Common Mistakes:
Omitting 'ARRAY' keyword in FOREACH
Wrong variable type for array elements
Misunderstanding RAISE NOTICE usage
5. You want to write a PL/pgSQL function that takes an integer array and returns the count of even numbers using FOREACH. Which of the following code snippets correctly implements this?
hard
A. CREATE FUNCTION count_evens(arr integer[]) RETURNS integer AS $$
DECLARE
val integer;
count integer := 0;
BEGIN
FOREACH val IN ARRAY arr LOOP
IF val % 2 = 0 THEN
count := count + 1;
END IF;
END LOOP;
RETURN count;
END;
$$ LANGUAGE plpgsql;
B. CREATE FUNCTION count_evens(arr integer[]) RETURNS integer AS $$
DECLARE
val integer;
count integer := 0;
BEGIN
FOREACH val FROM arr LOOP
IF val % 2 = 0 THEN
count := count + 1;
END IF;
END LOOP;
RETURN count;
END;
$$ LANGUAGE plpgsql;
C. CREATE FUNCTION count_evens(arr integer[]) RETURNS integer AS $$
DECLARE
val integer;
count integer := 0;
BEGIN
FOR val IN ARRAY arr LOOP
IF val % 2 = 0 THEN
count := count + 1;
END IF;
END LOOP;
RETURN count;
END;
$$ LANGUAGE plpgsql;
D. CREATE FUNCTION count_evens(arr integer[]) RETURNS integer AS $$
DECLARE
val integer;
count integer := 0;
BEGIN
FOREACH val ON ARRAY arr LOOP
IF val % 2 = 0 THEN
count := count + 1;
END IF;
END LOOP;
RETURN count;
END;
$$ LANGUAGE plpgsql;
Solution
Step 1: Check FOREACH syntax correctness
CREATE FUNCTION count_evens(arr integer[]) RETURNS integer AS $$
DECLARE
val integer;
count integer := 0;
BEGIN
FOREACH val IN ARRAY arr LOOP
IF val % 2 = 0 THEN
count := count + 1;
END IF;
END LOOP;
RETURN count;
END;
$$ LANGUAGE plpgsql; uses 'FOREACH val IN ARRAY arr LOOP', which is the correct syntax.
Step 2: Verify logic for counting even numbers
Inside the loop, it checks if val is even and increments count correctly.
Final Answer:
FOREACH val IN ARRAY arr LOOP with IF val % 2 = 0 logic -> Option A
Quick Check:
FOREACH ... IN ARRAY ... and correct IF condition [OK]
Hint: Use 'FOREACH val IN ARRAY arr' and check val % 2 = 0 [OK]