Challenge - 5 Problems
SETOF Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple SETOF function call
Consider the following PostgreSQL function that returns a set of integers. What will be the output of
SELECT * FROM generate_numbers(3);?PostgreSQL
CREATE OR REPLACE FUNCTION generate_numbers(n integer) RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT generate_series(1, n); END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
The function uses RETURN QUERY with generate_series from 1 to n.
✗ Incorrect
The function returns a set of integers from 1 up to the input n using generate_series. The SELECT * FROM function returns each integer as a separate row.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in a SETOF function
Which option contains a syntax error in defining a PostgreSQL function that returns SETOF text?
PostgreSQL
CREATE OR REPLACE FUNCTION list_names() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT name FROM users; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
RETURN QUERY is required to return multiple rows, RETURN alone returns a single value.
✗ Incorrect
Option D uses RETURN instead of RETURN QUERY, which causes a syntax error because RETURN expects a single value, not a query returning multiple rows.
❓ optimization
advanced2:00remaining
Optimizing a SETOF function for performance
You have a function that returns SETOF rows from a large table using a loop and RETURN NEXT. Which option is the best way to optimize it?
PostgreSQL
CREATE OR REPLACE FUNCTION get_active_users() RETURNS SETOF users AS $$ DECLARE rec users%ROWTYPE; BEGIN FOR rec IN SELECT * FROM users WHERE active = true LOOP RETURN NEXT rec; END LOOP; RETURN; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
RETURN QUERY can return all rows at once without looping.
✗ Incorrect
Using RETURN QUERY with a single SELECT is more efficient than looping with RETURN NEXT for large result sets.
🧠 Conceptual
advanced2:00remaining
Understanding the difference between RETURNS SETOF and RETURNS TABLE
Which statement correctly describes the difference between
RETURNS SETOF and RETURNS TABLE in PostgreSQL functions?Attempts:
2 left
💡 Hint
Think about how the output columns are defined and accessed.
✗ Incorrect
RETURNS TABLE allows you to define column names and types directly in the function signature, making it easier to SELECT from the function with named columns. RETURNS SETOF returns a set of a single type, often a composite type or scalar, without explicit column names.🔧 Debug
expert2:00remaining
Debugging a function returning SETOF with unexpected empty result
A function defined as below returns no rows when called, but the underlying SELECT query returns rows when run alone. What is the most likely cause?
PostgreSQL
CREATE OR REPLACE FUNCTION get_recent_orders() RETURNS SETOF orders AS $$ BEGIN RETURN QUERY SELECT * FROM orders WHERE order_date > CURRENT_DATE - INTERVAL '7 days'; RETURN NEXT NULL; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
Consider how RETURN NEXT NULL affects the result set.
✗ Incorrect
RETURN NEXT NULL adds a NULL row to the result set, but since it is after RETURN QUERY, it can cause confusion or unexpected results. In this case, it effectively overrides the previous rows, resulting in an empty or NULL-only result.