0
0
PostgreSQLquery~20 mins

Functions returning SETOF in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SETOF Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A
3
2
1
B
1
2
3
C1, 2, 3
DError: function does not return a set
Attempts:
2 left
💡 Hint
The function uses RETURN QUERY with generate_series from 1 to n.
📝 Syntax
intermediate
2: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;
ACREATE OR REPLACE FUNCTION list_names() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT name FROM users; END; $$ LANGUAGE plpgsql;
BCREATE OR REPLACE FUNCTION list_names() RETURNS SETOF text AS $$ BEGIN RETURN NEXT SELECT name FROM users; END; $$ LANGUAGE plpgsql;
C;lqsgplp EGAUGNAL $$ ;DNE ;sresu MORF eman TCELES YREUQ NRUTER NIGEB $$ SA txet FOTES SNRUTER )(seman_tsil NOITCNUF ECALPER RO ETAERC
DCREATE OR REPLACE FUNCTION list_names() RETURNS SETOF text AS $$ BEGIN RETURN 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.
optimization
advanced
2: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;
AReplace the loop with RETURN QUERY SELECT * FROM users WHERE active = true;
BAdd an index on the active column and keep the loop as is.
CChange RETURNS SETOF users to RETURNS TABLE and keep the loop.
DUse a cursor to fetch rows and RETURN NEXT inside the loop.
Attempts:
2 left
💡 Hint
RETURN QUERY can return all rows at once without looping.
🧠 Conceptual
advanced
2: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?
A<code>RETURNS TABLE</code> defines named columns and allows direct SELECT from the function; <code>RETURNS SETOF</code> returns a set of a single type without named columns.
B<code>RETURNS SETOF</code> can only return scalar types; <code>RETURNS TABLE</code> can return composite types.
C<code>RETURNS TABLE</code> functions cannot be used in FROM clauses; <code>RETURNS SETOF</code> functions can.
DThere is no difference; both are interchangeable in all cases.
Attempts:
2 left
💡 Hint
Think about how the output columns are defined and accessed.
🔧 Debug
expert
2: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;
AThe function should use <code>RETURN NEXT</code> inside a loop instead of RETURN QUERY.
BThe function is missing a <code>RETURN;</code> statement at the end.
CThe extra <code>RETURN NEXT NULL;</code> causes the function to return no rows.
DThe function's language should be SQL, not plpgsql.
Attempts:
2 left
💡 Hint
Consider how RETURN NEXT NULL affects the result set.