What if you could get fresh, filtered data instantly with just one simple call?
Why Functions returning TABLE in PostgreSQL? - Purpose & Use Cases
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.