CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT 1; END; $$ LANGUAGE plpgsql; -> Option C
Quick Check:
RETURNS SETOF + RETURN QUERY = CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT 1; END; $$ LANGUAGE plpgsql; [OK]
Hint: Use RETURNS SETOF and RETURN QUERY for multiple rows [OK]
Common Mistakes:
Using RETURNS integer instead of SETOF integer
Using RETURN instead of RETURN QUERY for sets
Confusing RETURNS TABLE with RETURNS SETOF
3.
Given this function:
CREATE FUNCTION get_even_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT generate_series(1,5) WHERE generate_series % 2 = 0; END; $$ LANGUAGE plpgsql;
What will SELECT * FROM get_even_numbers(); return?
medium
A. Empty set
B. [1, 3, 5]
C. [1, 2, 3, 4, 5]
D. [2, 4]
Solution
Step 1: Understand generate_series and filter
The function selects numbers from 1 to 5 but filters only even numbers using WHERE generate_series % 2 = 0.
Step 2: Identify even numbers in range
Even numbers between 1 and 5 are 2 and 4.
Final Answer:
[2, 4] -> Option D
Quick Check:
Even numbers 1-5 = [2,4] [OK]
Hint: Filter generate_series with modulo for evens [OK]
Common Mistakes:
Including odd numbers by mistake
Returning all numbers without filter
Expecting empty set due to syntax confusion
4.
Identify the error in this function that returns a set of text values:
CREATE FUNCTION get_names() RETURNS SETOF text AS $$ BEGIN RETURN SELECT name FROM users; END; $$ LANGUAGE plpgsql;
medium
A. Missing RETURN QUERY before SELECT statement.
B. RETURNS SETOF text is invalid syntax.
C. Function must return TABLE, not SETOF.
D. LANGUAGE plpgsql is not allowed for set-returning functions.
Solution
Step 1: Check how to return multiple rows in plpgsql
To return multiple rows, use RETURN QUERY SELECT ... inside the function.
Step 2: Identify missing keyword
The function uses RETURN SELECT ... which is invalid; it must be RETURN QUERY SELECT ....
Final Answer:
Missing RETURN QUERY before SELECT statement. -> Option A
Quick Check:
Use RETURN QUERY for sets [OK]
Hint: Use RETURN QUERY to return sets inside plpgsql [OK]
Common Mistakes:
Using RETURN instead of RETURN QUERY
Confusing RETURNS SETOF with RETURNS TABLE
Thinking LANGUAGE plpgsql disallows set returns
5.
You want to create a function that returns all employees with salary above a given amount. Which is the best way to write this function?
CREATE FUNCTION get_high_salary(min_salary numeric) RETURNS SETOF employees AS $$ BEGIN RETURN QUERY SELECT * FROM employees WHERE salary > min_salary; END; $$ LANGUAGE plpgsql;
What is the correct way to call this function to get all employees earning more than 50000?
hard
A. SELECT get_high_salary(50000);
B. SELECT * FROM get_high_salary(50000);
C. CALL get_high_salary(50000);
D. EXECUTE get_high_salary(50000);
Solution
Step 1: Understand function returns SETOF employees
The function returns multiple rows, so it must be called in a FROM clause to get rows.
Step 2: Choose correct call syntax
Use SELECT * FROM function_name(args); to get all rows returned by the function.
Final Answer:
SELECT * FROM get_high_salary(50000); -> Option B
Quick Check:
Call set-returning function in FROM clause [OK]
Hint: Call set-returning functions with SELECT * FROM [OK]