Complete the code to return a single integer value from the function.
CREATE FUNCTION get_five() RETURNS integer AS $$ BEGIN RETURN [1]; END; $$ LANGUAGE plpgsql;The RETURN statement returns a single value from the function. Here, returning the integer 5 is correct.
Complete the code to return multiple rows from the function using RETURN NEXT.
CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN [1] 1; [1] 2; RETURN; END; $$ LANGUAGE plpgsql;
RETURN NEXT adds a row to the result set in a set-returning function. Multiple RETURN NEXT statements add multiple rows.
Fix the error in the function to correctly return multiple text rows.
CREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN [1] 'hello'; [1] 'world'; RETURN; END; $$ LANGUAGE plpgsql;
To return multiple rows in a set-returning function, use RETURN NEXT for each row, then RETURN to finish.
Fill both blanks to return squares of numbers 1 to 3 using RETURN NEXT.
CREATE FUNCTION get_squares() RETURNS SETOF integer AS $$ DECLARE i integer := 1; BEGIN WHILE i <= 3 LOOP [1] i * i; i := i + 1; END LOOP; [2]; END; $$ LANGUAGE plpgsql;
Inside the loop, use RETURN NEXT to add each square to the result set. After the loop, use RETURN to finish the function.
Fill all three blanks to return uppercase words longer than 3 letters using RETURN NEXT.
CREATE FUNCTION filter_words(words text[]) RETURNS SETOF text AS $$ DECLARE w text; BEGIN FOREACH w IN ARRAY words LOOP IF length(w) [1] 3 THEN [2] upper(w); END IF; END LOOP; [3]; END; $$ LANGUAGE plpgsql;
The condition checks if word length is greater than 3 using >. Use RETURN NEXT to add the uppercase word to the result set. Finally, RETURN ends the function and returns all rows.