Bird
0
0

You need to write a PostgreSQL function that returns all products priced above a certain threshold as a set of rows. Which is the best approach?

hard📝 Application Q8 of 15
PostgreSQL - Advanced PL/pgSQL

You need to write a PostgreSQL function that returns all products priced above a certain threshold as a set of rows. Which is the best approach?

AUse RETURNS SETOF product and RETURN QUERY SELECT * FROM products WHERE price > threshold;
BUse RETURNS product and RETURN NEXT for each product inside a loop.
CUse RETURNS TABLE and RETURN a single row with aggregated data.
DUse RETURNS SETOF integer and return product IDs only.
Step-by-Step Solution
Solution:
  1. Step 1: Determine return type

    To return multiple product rows, RETURNS SETOF product is appropriate.
  2. Step 2: Use RETURN QUERY

    RETURN QUERY with SELECT filters products by price efficiently.
  3. Step 3: Analyze options

    Use RETURNS SETOF product and RETURN QUERY SELECT * FROM products WHERE price > threshold; uses correct return type and query method.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    RETURN QUERY with RETURNS SETOF best for multiple rows [OK]
Quick Trick: Use RETURNS SETOF with RETURN QUERY for multiple rows [OK]
Common Mistakes:
  • Using RETURNS scalar instead of SETOF
  • Returning only IDs when full rows needed
  • Using RETURN NEXT unnecessarily in simple queries

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes