Challenge - 5 Problems
VARIADIC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a function using VARIADIC integer array
Consider the following PostgreSQL function that sums all integers passed as VARIADIC parameters:
What is the output of the query
CREATE OR REPLACE FUNCTION sum_variadic(VARIADIC nums integer[]) RETURNS integer AS $$
DECLARE
total integer := 0;
n integer;
BEGIN
FOREACH n IN ARRAY nums LOOP
total := total + n;
END LOOP;
RETURN total;
END;
$$ LANGUAGE plpgsql;What is the output of the query
SELECT sum_variadic(1, 2, 3, 4);?PostgreSQL
CREATE OR REPLACE FUNCTION sum_variadic(VARIADIC nums integer[]) RETURNS integer AS $$ DECLARE total integer := 0; n integer; BEGIN FOREACH n IN ARRAY nums LOOP total := total + n; END LOOP; RETURN total; END; $$ LANGUAGE plpgsql; SELECT sum_variadic(1, 2, 3, 4);
Attempts:
2 left
💡 Hint
VARIADIC parameters allow passing multiple arguments as an array inside the function.
✗ Incorrect
The function sums all integers passed as VARIADIC parameters. The call passes 1, 2, 3, 4 which sum to 10.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax to declare a VARIADIC parameter
Which of the following is the correct way to declare a VARIADIC parameter in a PostgreSQL function that accepts text arguments?
Attempts:
2 left
💡 Hint
VARIADIC must appear before the parameter name and type as an array.
✗ Incorrect
The correct syntax is to write VARIADIC before the parameter name and specify the type as an array, e.g., VARIADIC args text[].
❓ optimization
advanced2:00remaining
Optimizing a function using VARIADIC parameters for concatenation
You have this function that concatenates text arguments passed as VARIADIC parameters:
Which option provides a more efficient way to concatenate all VARIADIC text arguments?
CREATE OR REPLACE FUNCTION concat_variadic(VARIADIC texts text[]) RETURNS text AS $$
DECLARE
result text := '';
t text;
BEGIN
FOREACH t IN ARRAY texts LOOP
result := result || t;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;Which option provides a more efficient way to concatenate all VARIADIC text arguments?
Attempts:
2 left
💡 Hint
PostgreSQL has built-in functions to join array elements efficiently.
✗ Incorrect
array_to_string(texts, '') concatenates all array elements efficiently without looping manually.
🧠 Conceptual
advanced2:00remaining
Understanding VARIADIC parameter behavior with arrays
Given this function:
What is the result of the query
CREATE OR REPLACE FUNCTION test_variadic(VARIADIC vals integer[]) RETURNS integer AS $$ BEGIN RETURN array_length(vals, 1); END; $$ LANGUAGE plpgsql;
What is the result of the query
SELECT test_variadic(ARRAY[1,2,3]);?PostgreSQL
CREATE OR REPLACE FUNCTION test_variadic(VARIADIC vals integer[]) RETURNS integer AS $$ BEGIN RETURN array_length(vals, 1); END; $$ LANGUAGE plpgsql; SELECT test_variadic(ARRAY[1,2,3]);
Attempts:
2 left
💡 Hint
Passing an array to a VARIADIC parameter treats the whole array as one argument unless VARIADIC is specified in the call.
✗ Incorrect
Passing ARRAY[1,2,3] as a single argument means vals is an array with one element (the array itself), so length is 1.
🔧 Debug
expert2:00remaining
Diagnose the error when calling a VARIADIC function
You have this function:
When you run
What is the cause of this error?
CREATE OR REPLACE FUNCTION multiply_variadic(VARIADIC nums integer[]) RETURNS integer AS $$
DECLARE
product integer := 1;
n integer;
BEGIN
FOREACH n IN ARRAY nums LOOP
product := product * n;
END LOOP;
RETURN product;
END;
$$ LANGUAGE plpgsql;When you run
SELECT multiply_variadic(ARRAY[2,3,4]); you get an error:ERROR: function multiply_variadic(integer[]) does not existWhat is the cause of this error?
Attempts:
2 left
💡 Hint
Check how VARIADIC functions are called with arrays.
✗ Incorrect
To pass an array to a VARIADIC parameter, you must use the VARIADIC keyword in the call: SELECT multiply_variadic(VARIADIC ARRAY[2,3,4]);