0
0
PostgreSQLquery~20 mins

Functions returning TABLE in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Table Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple TABLE function
Consider the following PostgreSQL function that returns a table of numbers and their squares.

What is the output of SELECT * FROM get_squares(3);?
PostgreSQL
CREATE OR REPLACE FUNCTION get_squares(n integer) RETURNS TABLE(num integer, square integer) AS $$
BEGIN
  RETURN QUERY SELECT i, i*i FROM generate_series(1, n) AS i;
END;
$$ LANGUAGE plpgsql;
A
num | square
----+--------
1   | 1
2   | 4
3   | 9
B
num | square
----+--------
1   | 1
2   | 2
3   | 3
C
num | square
----+--------
0   | 0
1   | 1
2   | 4
3   | 9
D
num | square
----+--------
1   | 1
4   | 16
9   | 81
Attempts:
2 left
💡 Hint
The function returns numbers from 1 to n and their squares.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in TABLE function
Which option contains the correct syntax to define a PostgreSQL function returning a table with columns id (integer) and name (text)?
ACREATE FUNCTION get_users() RETURNS TABLE(id integer, name text) AS $$ BEGIN RETURN QUERY SELECT user_id, user_name FROM users; END $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_users() RETURNS TABLE(id integer, name text) AS $$ RETURN QUERY SELECT user_id, user_name FROM users; $$ LANGUAGE plpgsql;
CCREATE FUNCTION get_users() RETURNS TABLE(id integer, name text) AS $$ BEGIN RETURN QUERY SELECT user_id, user_name FROM users; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_users() RETURNS TABLE(id integer, name text) AS $$ BEGIN RETURN SELECT user_id, user_name FROM users; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
A TABLE function must use RETURN QUERY inside a BEGIN...END block.
optimization
advanced
2:00remaining
Optimizing a TABLE function with a loop
Given this function that returns numbers and their factorials, which option optimizes it best by avoiding explicit loops?
PostgreSQL
CREATE OR REPLACE FUNCTION factorials(n integer) RETURNS TABLE(num integer, fact bigint) AS $$
DECLARE
  f bigint := 1;
BEGIN
  FOR i IN 1..n LOOP
    f := f * i;
    RETURN NEXT (i, f);
  END LOOP;
END;
$$ LANGUAGE plpgsql;
ACREATE OR REPLACE FUNCTION factorials(n integer) RETURNS TABLE(num integer, fact bigint) AS $$ BEGIN RETURN QUERY SELECT i, sum(i) FROM generate_series(1, n) AS i GROUP BY i; END; $$ LANGUAGE plpgsql;
BCREATE OR REPLACE FUNCTION factorials(n integer) RETURNS TABLE(num integer, fact bigint) AS $$ BEGIN RETURN QUERY SELECT i, factorial(i) FROM generate_series(1, n) AS i; END; $$ LANGUAGE plpgsql;
CCREATE OR REPLACE FUNCTION factorials(n integer) RETURNS TABLE(num integer, fact bigint) AS $$ BEGIN RETURN QUERY SELECT i, exp(sum(ln(i))) OVER (ORDER BY i) FROM generate_series(1, n) AS i; END; $$ LANGUAGE plpgsql;
DCREATE OR REPLACE FUNCTION factorials(n integer) RETURNS TABLE(num integer, fact bigint) AS $$ BEGIN RETURN QUERY SELECT i, i! FROM generate_series(1, n) AS i; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
PostgreSQL has a built-in factorial function.
🔧 Debug
advanced
2:00remaining
Debugging a TABLE function with missing RETURN QUERY
What error will this function raise when called?

CREATE OR REPLACE FUNCTION get_data() RETURNS TABLE(id integer, val text) AS $$ BEGIN SELECT id, value FROM data_table; END; $$ LANGUAGE plpgsql;
ANo error, returns all rows from data_table
BERROR: syntax error at or near "SELECT"
CFunction returns no rows
DERROR: query has no destination for result data
Attempts:
2 left
💡 Hint
In PL/pgSQL, SELECT without INTO or RETURN QUERY causes error.
🧠 Conceptual
expert
2:00remaining
Understanding TABLE function behavior with OUT parameters
Which statement about PostgreSQL functions returning TABLE with OUT parameters is true?
AFunctions with OUT parameters automatically return a table without needing RETURN QUERY.
BFunctions with OUT parameters must always use RETURN NEXT to return rows.
CFunctions with OUT parameters cannot return multiple rows.
DFunctions with OUT parameters require explicit RETURNS SETOF type instead of TABLE.
Attempts:
2 left
💡 Hint
OUT parameters define the output columns and the function returns rows automatically.