Bird
0
0

How would you modify this function to return only users with IDs greater than 10?

hard📝 Application Q9 of 15
PostgreSQL - Advanced PL/pgSQL
How would you modify this function to return only users with IDs greater than 10?
CREATE FUNCTION filtered_users() RETURNS TABLE(id INT, name TEXT) AS $$ BEGIN RETURN QUERY SELECT id, name FROM users; END; $$ LANGUAGE plpgsql;
AAdd IF id > 10 THEN RETURN NEXT id, name; END IF;
BAdd a parameter to the function and filter outside
CChange RETURNS TABLE to RETURNS SETOF RECORD
DAdd WHERE id > 10 in the SELECT inside RETURN QUERY
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering in RETURN QUERY

    To filter rows, add a WHERE clause inside the SELECT statement used in RETURN QUERY.
  2. Step 2: Evaluate other options

    Add IF id > 10 THEN RETURN NEXT id, name; END IF; is invalid syntax, C changes return type unnecessarily, and D is a design change, not a direct fix.
  3. Final Answer:

    Add WHERE id > 10 in the SELECT inside RETURN QUERY -> Option D
  4. Quick Check:

    Filter rows inside RETURN QUERY SELECT with WHERE clause = B [OK]
Quick Trick: Filter rows inside RETURN QUERY SELECT with WHERE clause [OK]
Common Mistakes:
  • Trying to filter with IF inside function body incorrectly
  • Changing return type unnecessarily
  • Filtering outside function instead of inside

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes