0
0
PostgreSQLquery~10 mins

VARIADIC parameters in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function that accepts a variadic integer array parameter.

PostgreSQL
CREATE FUNCTION sum_numbers([1] integer[]) RETURNS integer AS $$ BEGIN RETURN 0; END; $$ LANGUAGE plpgsql;
Drag options to blanks, or click blank then click option'
Anumbers VARIADIC
Bnumbers
CVARIADIC numbers
DVARIADIC
Attempts:
3 left
💡 Hint
Common Mistakes
Placing VARIADIC after the parameter name.
Omitting the VARIADIC keyword.
Using VARIADIC without a parameter name.
2fill in blank
medium

Complete the function call to pass multiple integers as a variadic argument.

PostgreSQL
SELECT sum_numbers([1]);
Drag options to blanks, or click blank then click option'
AVARIADIC ARRAY[1, 2, 3]
BARRAY[1, 2, 3]
C1, 2, 3
DVARIADIC 1, 2, 3
Attempts:
3 left
💡 Hint
Common Mistakes
Passing values without VARIADIC keyword.
Passing values separated by commas without ARRAY.
Using VARIADIC without ARRAY.
3fill in blank
hard

Fix the error in the function definition to correctly use a variadic text parameter.

PostgreSQL
CREATE FUNCTION concat_texts([1] text) RETURNS text AS $$ BEGIN RETURN ''; END; $$ LANGUAGE plpgsql;
Drag options to blanks, or click blank then click option'
AVARIADIC texts
BVARIADIC texts text[]
Ctexts VARIADIC
Dtexts text VARIADIC
Attempts:
3 left
💡 Hint
Common Mistakes
Placing VARIADIC after the parameter name.
Using text[] instead of text as type.
Omitting VARIADIC keyword.
4fill in blank
hard

Fill both blanks to create a function that sums variadic integers and returns the total.

PostgreSQL
CREATE FUNCTION sum_all([1] integer) RETURNS integer AS $$ DECLARE total integer := 0; BEGIN FOREACH num IN ARRAY [2] LOOP total := total + num; END LOOP; RETURN total; END; $$ LANGUAGE plpgsql;
Drag options to blanks, or click blank then click option'
AVARIADIC nums
Bnums
Attempts:
3 left
💡 Hint
Common Mistakes
Not using VARIADIC in the parameter.
Looping over the wrong variable name.
Using the parameter as a scalar instead of an array.
5fill in blank
hard

Fill all three blanks to define and call a variadic function that concatenates texts with a separator.

PostgreSQL
CREATE FUNCTION concat_with_sep([1] text, [2] text) RETURNS text AS $$ BEGIN RETURN array_to_string([3], sep); END; $$ LANGUAGE plpgsql; SELECT concat_with_sep(',', VARIADIC ARRAY['a', 'b', 'c']);
Drag options to blanks, or click blank then click option'
Asep
BVARIADIC texts
Ctexts
Dtexts[]
Attempts:
3 left
💡 Hint
Common Mistakes
Not using VARIADIC for the second parameter.
Using the wrong variable name inside the function.
Declaring the separator as variadic.