Complete the code to declare a function that returns a table.
CREATE FUNCTION get_users() RETURNS [1] AS $$ BEGIN RETURN QUERY SELECT id, name FROM users; END; $$ LANGUAGE plpgsql;The function returns a table with columns id and name. So RETURNS TABLE(id INT, name TEXT) is correct.
Complete the code to return rows from the function.
CREATE FUNCTION get_active_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN [1] SELECT id, name FROM users WHERE active = true; END; $$ LANGUAGE plpgsql;In PL/pgSQL, RETURN QUERY is used to return the result of a query from a function returning TABLE.
Fix the error in the function that returns a table.
CREATE FUNCTION get_scores() RETURNS TABLE(player TEXT, score INT) AS $$ BEGIN [1] SELECT player, score FROM scores; END; $$ LANGUAGE plpgsql;The function must use RETURN QUERY to return multiple rows from the SELECT statement.
Fill both blanks to define a function returning a table with two columns and returning rows.
CREATE FUNCTION get_products() RETURNS [1] AS $$ BEGIN [2] SELECT id, name FROM products; END; $$ LANGUAGE plpgsql;
The function returns a table with columns, so use RETURNS TABLE(id INT, name TEXT). To return rows, use RETURN QUERY.
Fill all three blanks to create a function returning a table with columns, returning rows, and specifying language.
CREATE FUNCTION get_orders() RETURNS [1] AS $$ BEGIN [2] SELECT order_id, total FROM orders; END; $$ LANGUAGE [3];
The function returns a table with columns, so use RETURNS TABLE(order_id INT, total NUMERIC). Use RETURN QUERY to return rows. The language is plpgsql.