Bird
0
0

Why might a PostgreSQL function declared with RETURNS SETOF use RETURN QUERY instead of multiple RETURN NEXT statements?

hard📝 Conceptual Q10 of 15
PostgreSQL - Advanced PL/pgSQL

Why might a PostgreSQL function declared with RETURNS SETOF use RETURN QUERY instead of multiple RETURN NEXT statements?

ARETURN QUERY is required for functions returning scalar values
BRETURN QUERY executes a single query returning all rows at once, improving performance
CRETURN NEXT can only return one row total
DRETURN NEXT causes syntax errors in SETOF functions
Step-by-Step Solution
Solution:
  1. Step 1: Compare RETURN QUERY and RETURN NEXT

    RETURN QUERY runs a single query returning all rows at once; RETURN NEXT adds rows one by one.
  2. Step 2: Consider performance and simplicity

    RETURN QUERY is more efficient and simpler for returning many rows from a query.
  3. Final Answer:

    RETURN QUERY executes a single query returning all rows at once, improving performance -> Option B
  4. Quick Check:

    RETURN QUERY is efficient for bulk row returns [OK]
Quick Trick: RETURN QUERY returns all rows at once, better for performance [OK]
Common Mistakes:
  • Thinking RETURN NEXT can only return one row total
  • Confusing RETURN QUERY with scalar returns
  • Believing RETURN NEXT causes syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes