0
0
PostgreSQLquery~20 mins

VARIADIC parameters in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
VARIADIC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a function using VARIADIC integer array
Consider the following PostgreSQL function that sums all integers passed as VARIADIC parameters:

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);
A10
BARRAY[1,2,3,4]
C1
DERROR: function sum_variadic(integer, integer, integer, integer) does not exist
Attempts:
2 left
💡 Hint
VARIADIC parameters allow passing multiple arguments as an array inside the function.
📝 Syntax
intermediate
2: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?
ACREATE FUNCTION example(args text VARIADIC) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION example(args VARIADIC text[]) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION example(VARIADIC args text[]) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION example(VARIADIC args text) RETURNS void AS $$ BEGIN END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
VARIADIC must appear before the parameter name and type as an array.
optimization
advanced
2:00remaining
Optimizing a function using VARIADIC parameters for concatenation
You have this function that concatenates text arguments passed as VARIADIC parameters:

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?
AConvert texts to JSON and then concatenate the JSON strings.
BUse a nested loop to concatenate each character of each string.
CUse string_agg(texts, '') inside the function.
DUse array_to_string(texts, '') to concatenate all elements at once.
Attempts:
2 left
💡 Hint
PostgreSQL has built-in functions to join array elements efficiently.
🧠 Conceptual
advanced
2:00remaining
Understanding VARIADIC parameter behavior with arrays
Given this function:

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]);
A1
B3
CNULL
DERROR: function test_variadic(integer[]) does not exist
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.
🔧 Debug
expert
2:00remaining
Diagnose the error when calling a VARIADIC function
You have this function:

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 exist

What is the cause of this error?
AThe function does not accept any arguments, so passing an array causes an error.
BThe function expects separate integer arguments, but an integer array was passed without VARIADIC keyword.
CThe function expects a single integer, but an array was passed.
DThe function is missing a RETURNS clause causing the error.
Attempts:
2 left
💡 Hint
Check how VARIADIC functions are called with arrays.