Complete the code to declare a function that returns a set of integers.
CREATE FUNCTION get_numbers() RETURNS [1] AS $$ BEGIN RETURN QUERY SELECT generate_series(1, 5); END; $$ LANGUAGE plpgsql;
The function must return a set of integers, so RETURNS SETOF integer is correct.
Complete the code to return rows from the function using RETURN QUERY.
CREATE FUNCTION get_names() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT [1] FROM unnest(array['Alice', 'Bob', 'Carol']) AS names(name); END; $$ LANGUAGE plpgsql;
The alias for the unnest array is names(name), so selecting name returns the text values.
Fix the error in the function to correctly return a set of rows from a table.
CREATE FUNCTION get_employees() RETURNS SETOF employees AS $$ BEGIN RETURN QUERY SELECT * FROM [1]; END; $$ LANGUAGE plpgsql;The function returns SETOF employees, so the table name must be employees to match the type.
Fill both blanks to create a function returning a set of rows with a WHERE condition.
CREATE FUNCTION get_active_users() RETURNS SETOF users AS $$ BEGIN RETURN QUERY SELECT * FROM users WHERE status = [1]; END; $$ LANGUAGE [2];
The function filters users with status 'active' and uses the plpgsql language.
Fill all three blanks to create a function returning a filtered set of rows with a parameter.
CREATE FUNCTION get_orders_by_customer([1] integer) RETURNS SETOF orders AS $$ BEGIN RETURN QUERY SELECT * FROM orders WHERE customer_id = [2]; END; $$ LANGUAGE [3];
The parameter name is cust_id, used in the WHERE clause, and the function language is plpgsql.