Complete the code to create a simple FOR loop that iterates from 1 to 5.
DO $$ BEGIN FOR i IN 1..[1] LOOP RAISE NOTICE 'Number: %', i; END LOOP; END $$;
The FOR loop runs from 1 to 5, so the upper limit should be 5.
Complete the code to create a WHILE loop that counts down from 3 to 1.
DO $$ DECLARE counter INTEGER := 3; BEGIN WHILE counter > [1] LOOP RAISE NOTICE 'Count: %', counter; counter := counter - 1; END LOOP; END $$;
The loop continues while counter is greater than 0, counting down from 3 to 1.
Fix the error in the FOR loop that should iterate over an array of integers.
DO $$ DECLARE numbers INTEGER[] := ARRAY[2, 4, 6]; num INTEGER; BEGIN FOR num IN [1] LOOP RAISE NOTICE 'Value: %', num; END LOOP; END $$;
To loop over array elements in PostgreSQL, use UNNEST(array) in the FOR loop.
Fill both blanks to create a FOR loop that sums numbers from 1 to 4.
DO $$ DECLARE total INTEGER := 0; BEGIN FOR i IN [1] LOOP total := total [2] i; END LOOP; RAISE NOTICE 'Sum: %', total; END $$;
The loop runs from 1 to 4, adding each number to total using the + operator.
Fill all three blanks to create a WHILE loop that multiplies a number by 2 until it is greater than 20.
DO $$ DECLARE val INTEGER := [1]; BEGIN WHILE val [2] [3] LOOP RAISE NOTICE 'Value: %', val; val := val * 2; END LOOP; END $$;
Start val at 1, loop while val is less than or equal to 20, doubling val each time.