0
0
PostgreSQLquery~5 mins

Functions returning TABLE in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does a PostgreSQL function returning TABLE do?
It returns a set of rows with defined columns, like a table, allowing you to get multiple rows and columns from a single function call.
Click to reveal answer
beginner
How do you define the columns returned by a function returning TABLE in PostgreSQL?
You specify the column names and their data types in the RETURNS TABLE clause of the function definition.
Click to reveal answer
intermediate
What SQL statement inside a function returning TABLE provides the rows to return?
A RETURN QUERY statement is used to return the rows from a SELECT or other query inside the function.
Click to reveal answer
intermediate
Can a function returning TABLE be used in a FROM clause like a regular table?
Yes, you can call the function in the FROM clause and treat its output like a table to join or filter results.
Click to reveal answer
advanced
What is the benefit of using functions returning TABLE over returning SETOF RECORD?
Functions returning TABLE have named columns and types defined upfront, making them easier to use and safer because the output structure is fixed.
Click to reveal answer
Which keyword defines the columns and types a PostgreSQL function will return as a table?
ARETURNS TABLE
BRETURNS SETOF
CRETURNS RECORD
DRETURNS ROW
Inside a function returning TABLE, which statement returns the query result rows?
ARETURN ROW
BRETURN NEXT
CRETURN SET
DRETURN QUERY
How can you use a function returning TABLE in a SQL query?
AIn the FROM clause like a table
BOnly in the WHERE clause
COnly in the SELECT list
DOnly in the ORDER BY clause
What is a key advantage of RETURNS TABLE over RETURNS SETOF RECORD?
ARETURNS TABLE is faster to execute
BRETURNS TABLE defines output columns explicitly
CRETURNS TABLE can return scalar values only
DRETURNS TABLE does not support multiple rows
Which of these is a valid way to define a function returning TABLE in PostgreSQL?
ACREATE FUNCTION get_users() RETURNS VOID AS $$ BEGIN END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_users() RETURNS INT AS $$ SELECT id FROM users; $$ LANGUAGE sql;
CCREATE FUNCTION get_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, name FROM users; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_users() RETURNS RECORD AS $$ SELECT id, name FROM users; $$ LANGUAGE sql;
Explain how to create a PostgreSQL function that returns multiple rows and columns using RETURNS TABLE.
Think about how to define output columns and how to return query results inside the function.
You got /3 concepts.
    Describe the difference between RETURNS TABLE and RETURNS SETOF RECORD in PostgreSQL functions.
    Consider how output structure is handled in both cases.
    You got /3 concepts.