What if you could get fresh, filtered data instantly with just one simple call?
Why Functions returning TABLE in PostgreSQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a big spreadsheet with sales data, and you want to find all sales above a certain amount. You try to do this by copying data into new sheets and manually filtering rows every time you need an update.
This manual filtering is slow and tiring. You might make mistakes copying data, and every time new sales come in, you have to repeat the whole process. It's easy to lose track or miss important rows.
Functions returning TABLE let you write a reusable query that acts like a mini-database inside your database. You just call the function with your filter, and it returns exactly the rows you want, fresh and correct every time.
SELECT * FROM sales WHERE amount > 1000; -- run manually each timeCREATE FUNCTION get_big_sales(min_amount numeric) RETURNS TABLE(id int, amount numeric) AS $$
BEGIN
RETURN QUERY SELECT id, amount FROM sales WHERE amount > min_amount;
END;
$$ LANGUAGE plpgsql;
SELECT * FROM get_big_sales(1000);You can build powerful, reusable queries that return tables, making your data work for you automatically and reliably.
A store manager can quickly get a list of all customers who spent more than $500 last month by calling a function, without digging through raw data or spreadsheets.
Manual filtering is slow and error-prone.
Functions returning TABLE automate and simplify data retrieval.
They make queries reusable and easy to maintain.
Practice
RETURNS TABLE do?Solution
Step 1: Understand the purpose of RETURNS TABLE
TheRETURNS TABLEclause defines that the function will return a set of rows with specified columns, like a table.Step 2: Compare with other return types
Unlike scalar returns or void,RETURNS TABLEreturns multiple rows and columns.Final Answer:
Returns multiple rows and columns as a table result -> Option CQuick Check:
RETURNS TABLE means multiple rows/columns [OK]
- Thinking RETURNS TABLE returns a single value
- Confusing RETURNS TABLE with RETURNS VOID
- Assuming it returns JSON automatically
id INT and name TEXT?Solution
Step 1: Check RETURNS TABLE syntax
CREATE FUNCTION f() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT 1, 'a'; END; $$ LANGUAGE plpgsql; correctly declaresRETURNS TABLE(id INT, name TEXT)matching the column names and types.Step 2: Verify RETURN QUERY usage
CREATE FUNCTION f() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT 1, 'a'; END; $$ LANGUAGE plpgsql; usesRETURN QUERY SELECT 1, 'a';which returns rows matching the table structure.Final Answer:
Correct RETURNS TABLE syntax and return statement -> Option DQuick Check:
RETURNS TABLE with matching columns and RETURN QUERY [OK]
- Using RETURNS SETOF RECORD without column definition
- Swapping column types in RETURNS TABLE
- Returning scalar instead of query
CREATE FUNCTION get_numbers() RETURNS TABLE(num INT) AS $$ BEGIN RETURN QUERY SELECT generate_series(1,3); END; $$ LANGUAGE plpgsql;
What will be the output of
SELECT * FROM get_numbers();?Solution
Step 1: Understand generate_series(1,3)
This function generates rows with values 1, 2, and 3.Step 2: RETURN QUERY returns all rows
The function returns all rows fromgenerate_series(1,3)as a table with columnnum.Final Answer:
3 rows with values 1, 2, 3 -> Option AQuick Check:
generate_series(1,3) returns 3 rows [OK]
- Thinking it returns a single row
- Confusing RETURNS TABLE with scalar return
- Expecting an error due to missing RETURNS SETOF
CREATE FUNCTION get_data() RETURNS TABLE(id INT, val TEXT) AS $$ BEGIN RETURN SELECT 1, 'a'; END; $$ LANGUAGE plpgsql;
Solution
Step 1: Check RETURN statement in RETURNS TABLE function
In PL/pgSQL, to return rows from a query, useRETURN QUERY, not justRETURN.Step 2: Identify missing RETURN QUERY
The function usesRETURN SELECTwhich is invalid syntax; it should beRETURN QUERY SELECT.Final Answer:
Missing RETURN QUERY before SELECT -> Option AQuick Check:
Use RETURN QUERY to return rows in RETURNS TABLE [OK]
- Using RETURN instead of RETURN QUERY for table results
- Omitting LANGUAGE plpgsql
- Incorrect RETURNS TABLE column types
users table with columns id INT and name TEXT. Which function definition correctly achieves this?Solution
Step 1: Match RETURNS TABLE columns with SELECT output
The function returnsid INTandname TEXT, so the SELECT must output these types. CREATE FUNCTION get_upper_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, UPPER(name) FROM users; END; $$ LANGUAGE plpgsql; matches this.Step 2: Check the transformation applied
CREATE FUNCTION get_upper_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, UPPER(name) FROM users; END; $$ LANGUAGE plpgsql; usesUPPER(name)to convert names to uppercase as required.Step 3: Verify other options
CREATE FUNCTION get_upper_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, LOWER(name) FROM users; END; $$ LANGUAGE plpgsql; uses LOWER instead of UPPER, CREATE FUNCTION get_upper_users() RETURNS SETOF record AS $$ BEGIN RETURN QUERY SELECT id, UPPER(name) FROM users; END; $$ LANGUAGE plpgsql; returns SETOF record (no column definition), CREATE FUNCTION get_upper_users() RETURNS TABLE(id TEXT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, UPPER(name) FROM users; END; $$ LANGUAGE plpgsql; mismatches id type (TEXT instead of INT).Final Answer:
Correctly returns id and uppercase name as a table -> Option BQuick Check:
RETURNS TABLE with matching columns and UPPER(name) [OK]
- Using LOWER instead of UPPER
- Mismatching column types in RETURNS TABLE
- Using SETOF record instead of RETURNS TABLE
