0
0
PostgreSQLquery~20 mins

FOREACH for array iteration in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FOREACH Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of FOREACH loop with integer array
What will be the output of the following PL/pgSQL block?
DO $$
DECLARE
  nums integer[] := ARRAY[1, 2, 3];
  total integer := 0;
  num integer;
BEGIN
  FOREACH num IN ARRAY nums LOOP
    total := total + num;
  END LOOP;
  RAISE NOTICE 'Total: %', total;
END $$;
PostgreSQL
DO $$
DECLARE
  nums integer[] := ARRAY[1, 2, 3];
  total integer := 0;
  num integer;
BEGIN
  FOREACH num IN ARRAY nums LOOP
    total := total + num;
  END LOOP;
  RAISE NOTICE 'Total: %', total;
END $$;
ATotal: 3
BTotal: 0
CTotal: 6
DSyntax error: missing variable declaration
Attempts:
2 left
💡 Hint
Think about how FOREACH iterates over each element and adds it to total.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in FOREACH usage
Which option contains a syntax error in using FOREACH to iterate over a text array in PL/pgSQL?
PostgreSQL
DECLARE
  fruits text[] := ARRAY['apple', 'banana', 'cherry'];
  fruit text;
BEGIN
  FOREACH fruit IN fruits LOOP
    RAISE NOTICE '%', fruit;
  END LOOP;
END;
AFOREACH fruit IN fruits LOOP
BFOREACH fruit IN fruit LOOP
CFOREACH fruit IN ARRAY fruits LOOP
DPOOL stiurf YARRA NI tiurf HCAEROF
Attempts:
2 left
💡 Hint
Check the correct syntax for iterating over arrays with FOREACH.
optimization
advanced
2:00remaining
Optimizing FOREACH loop for large arrays
You have a large integer array and want to sum its elements efficiently in PL/pgSQL. Which approach is the most efficient?
AUse FOREACH loop to add each element to a total variable
BUse unnest() in a SELECT SUM() query on the array
CConvert array to string and parse numbers manually
DUse FOREACH loop with RAISE NOTICE for each element
Attempts:
2 left
💡 Hint
Consider built-in SQL functions for aggregation over arrays.
🔧 Debug
advanced
2:00remaining
Debugging FOREACH loop with NULL elements
Given the array ARRAY[1, NULL, 3], what will be the value of total after this PL/pgSQL block?
DECLARE
  nums integer[] := ARRAY[1, NULL, 3];
  total integer := 0;
  num integer;
BEGIN
  FOREACH num IN ARRAY nums LOOP
    total := total + num;
  END LOOP;
  RAISE NOTICE 'Total: %', total;
END;
ATotal: 4
BRuntime error: cannot add NULL
CTotal: 0
DTotal: NULL
Attempts:
2 left
💡 Hint
Think about how NULL values affect arithmetic in SQL.
🧠 Conceptual
expert
2:00remaining
Understanding FOREACH loop variable scope
In PL/pgSQL, what is the scope of the loop variable declared in a FOREACH loop iterating over an array?
AThe loop variable must be declared outside the loop and retains its value after loop ends
BThe loop variable is global to the entire function
CThe loop variable is local to the FOREACH loop block only
DThe loop variable is automatically declared and cannot be accessed outside the loop
Attempts:
2 left
💡 Hint
Consider variable visibility rules in PL/pgSQL blocks.