Bird
0
0

Which of the following is the correct way to declare a PostgreSQL function that returns a set of text values?

easy📝 Syntax Q3 of 15
PostgreSQL - Advanced PL/pgSQL

Which of the following is the correct way to declare a PostgreSQL function that returns a set of text values?

CREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT ...; END; $$ LANGUAGE plpgsql;
ACREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_words() RETURNS text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION get_words() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN NEXT word FROM dictionary; END; $$ LANGUAGE plpgsql;
Step-by-Step Solution
Solution:
  1. Step 1: Check RETURNS clause

    Function must declare RETURNS SETOF text to return multiple text rows.
  2. Step 2: Validate RETURN QUERY usage

    RETURN QUERY with SELECT is correct to return multiple rows.
  3. Step 3: Analyze options

    CREATE FUNCTION get_words() RETURNS SETOF text AS $$ BEGIN RETURN QUERY SELECT word FROM dictionary; END; $$ LANGUAGE plpgsql; correctly uses RETURNS SETOF text and RETURN QUERY SELECT.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    RETURNS SETOF text with RETURN QUERY SELECT is correct [OK]
Quick Trick: Use RETURNS SETOF text with RETURN QUERY SELECT [OK]
Common Mistakes:
  • Omitting SETOF in RETURNS clause
  • Using wrong return type like integer
  • Using RETURN NEXT incorrectly with SELECT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes