Bird
0
0

Which of the following is the correct syntax to declare a PostgreSQL function returning a table with columns id INT and name TEXT?

easy📝 Syntax Q12 of 15
PostgreSQL - Advanced PL/pgSQL
Which of the following is the correct syntax to declare a PostgreSQL function returning a table with columns id INT and name TEXT?
ACREATE FUNCTION f() RETURNS TABLE(id TEXT, name INT) AS $$ BEGIN RETURN QUERY SELECT 1, 'a'; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION f() RETURNS SETOF RECORD AS $$ BEGIN RETURN QUERY SELECT 1, 'a'; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION f() RETURNS INT AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION f() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT 1, 'a'; END; $$ LANGUAGE plpgsql;
Step-by-Step Solution
Solution:
  1. 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 declares RETURNS TABLE(id INT, name TEXT) matching the column names and types.
  2. 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; uses RETURN QUERY SELECT 1, 'a'; which returns rows matching the table structure.
  3. Final Answer:

    Correct RETURNS TABLE syntax and return statement -> Option D
  4. Quick Check:

    RETURNS TABLE with matching columns and RETURN QUERY [OK]
Quick Trick: RETURNS TABLE needs column names/types and RETURN QUERY [OK]
Common Mistakes:
  • Using RETURNS SETOF RECORD without column definition
  • Swapping column types in RETURNS TABLE
  • Returning scalar instead of query

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes