Bird
0
0

What will be the output of this function call?

medium📝 query result Q5 of 15
PostgreSQL - PL/pgSQL Fundamentals
What will be the output of this function call?
CREATE FUNCTION sample() RETURNS SETOF text AS $$
BEGIN
  RETURN NEXT 'apple';
  RETURN NEXT 'banana';
  RETURN 'cherry';
END;
$$ LANGUAGE plpgsql;

SELECT * FROM sample();
ARows: 'apple', 'banana'
BRow: 'cherry' only
CRows: 'apple', 'banana', 'cherry'
DError: RETURN used after RETURN NEXT
Step-by-Step Solution
Solution:
  1. Step 1: Analyze RETURN NEXT statements

    First two RETURN NEXT add 'apple' and 'banana' to output.
  2. Step 2: Analyze RETURN statement

    RETURN 'cherry' adds 'cherry' to the output and ends the function.
  3. Final Answer:

    All three rows 'apple', 'banana', and 'cherry' are returned -> Option C
  4. Quick Check:

    RETURN adds final row and ends function = Rows: 'apple', 'banana', 'cherry' [OK]
Quick Trick: RETURN NEXT adds rows; RETURN adds final row and ends function [OK]
Common Mistakes:
  • Assuming only 'cherry' is returned
  • Thinking RETURN after RETURN NEXT ignores previous rows
  • Expecting error due to RETURN after RETURN NEXT

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes