Bird
Raised Fist0
PostgreSQLquery~20 mins

OUT parameters in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
OUT Parameters Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this function call with OUT parameters?

Consider the following PostgreSQL function:

CREATE OR REPLACE FUNCTION get_employee_info(emp_id INT, OUT emp_name TEXT, OUT emp_salary NUMERIC) AS $$
BEGIN
  SELECT name, salary INTO emp_name, emp_salary FROM employees WHERE id = emp_id;
END;
$$ LANGUAGE plpgsql;

What will be the result of SELECT * FROM get_employee_info(3); if the employee with id=3 is named 'Alice' with salary 75000?

PostgreSQL
CREATE OR REPLACE FUNCTION get_employee_info(emp_id INT, OUT emp_name TEXT, OUT emp_salary NUMERIC) AS $$
BEGIN
  SELECT name, salary INTO emp_name, emp_salary FROM employees WHERE id = emp_id;
END;
$$ LANGUAGE plpgsql;
A
| get_employee_info |
|-------------------|
| (Alice,75000)     |
B
| emp_name | emp_salary |
|----------|------------|
| Alice    | 75000      |
C
| emp_name | emp_salary |
|----------|------------|
| NULL     | NULL       |
DERROR: function get_employee_info(integer) does not exist
Attempts:
2 left
💡 Hint

OUT parameters in PostgreSQL functions return a row with named columns.

🧠 Conceptual
intermediate
1:30remaining
What is the main difference between OUT parameters and RETURNS TABLE in PostgreSQL functions?

Choose the correct statement about OUT parameters versus RETURNS TABLE in PostgreSQL functions.

AOUT parameters can only return one value; RETURNS TABLE can return multiple rows.
BOUT parameters require a RETURN statement; RETURNS TABLE does not.
COUT parameters define output variables and implicitly create a result set; RETURNS TABLE explicitly defines the result columns and types.
DOUT parameters are deprecated and should not be used; RETURNS TABLE is the modern replacement.
Attempts:
2 left
💡 Hint

Think about how output columns are defined and returned in each method.

📝 Syntax
advanced
2:30remaining
Which function definition correctly uses OUT parameters to return two values?

Identify the correct PostgreSQL function syntax that uses OUT parameters to return a user's first and last name.

ACREATE FUNCTION get_names(user_id INT, OUT first_name TEXT, OUT last_name TEXT) RETURNS RECORD AS $$ BEGIN SELECT fname, lname INTO first_name, last_name FROM users WHERE id = user_id; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_names(user_id INT) RETURNS TABLE(first_name TEXT, last_name TEXT) AS $$ BEGIN SELECT fname, lname FROM users WHERE id = user_id; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION get_names(user_id INT, OUT first_name TEXT, OUT last_name TEXT) AS $$ BEGIN SELECT fname, lname FROM users WHERE id = user_id INTO first_name, last_name; END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_names(user_id INT, OUT first_name TEXT, OUT last_name TEXT) AS $$ BEGIN SELECT fname, lname INTO first_name, last_name FROM users WHERE id = user_id; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint

Check the placement of INTO and the RETURNS clause for OUT parameters.

🔧 Debug
advanced
2:00remaining
Why does this function with OUT parameters raise an error?

Given this function:

CREATE FUNCTION get_product_info(p_id INT, OUT p_name TEXT, OUT p_price NUMERIC) AS $$
BEGIN
  SELECT name, price INTO p_name FROM products WHERE id = p_id;
END;
$$ LANGUAGE plpgsql;

Why does calling SELECT * FROM get_product_info(10); raise an error?

ABecause the SELECT INTO only assigns p_name but not p_price, so p_price remains uninitialized causing an error.
BBecause the function is missing a RETURN statement to return the OUT parameters.
CBecause the function should use RETURNS TABLE instead of OUT parameters.
DBecause the function is missing a semicolon after the SELECT statement.
Attempts:
2 left
💡 Hint

Check if all OUT parameters are assigned values before function ends.

optimization
expert
3:00remaining
How to optimize a function with multiple OUT parameters to avoid multiple queries?

You have a function with three OUT parameters that fetch data from the same table. Which approach optimizes performance best?

AUse a single SELECT statement with SELECT ... INTO to assign all OUT parameters at once.
BUse three separate SELECT statements, each assigning one OUT parameter.
CUse RETURNS TABLE and perform three separate queries inside the function.
DUse OUT parameters but assign values using multiple UPDATE statements inside the function.
Attempts:
2 left
💡 Hint

Think about minimizing the number of queries to the database.

Practice

(1/5)
1. What is the main purpose of OUT parameters in PostgreSQL functions?
easy
A. To define the function's return type as a single value
B. To declare input variables for the function
C. To create temporary tables inside the function
D. To allow a function to return multiple values as columns

Solution

  1. Step 1: Understand OUT parameters role

    OUT parameters are used to return multiple values from a function as separate columns.
  2. Step 2: Compare with other options

    Input variables are declared with IN, not OUT. Temporary tables and single return types are unrelated to OUT parameters.
  3. Final Answer:

    To allow a function to return multiple values as columns -> Option D
  4. Quick Check:

    OUT parameters = multiple return columns [OK]
Hint: OUT parameters return multiple columns from a function [OK]
Common Mistakes:
  • Confusing OUT with IN parameters
  • Thinking OUT creates tables
  • Assuming OUT returns a single value
2. Which of the following is the correct syntax to declare an OUT parameter in a PostgreSQL function?
easy
A. CREATE FUNCTION f(a INT OUT) RETURNS VOID AS $$ ... $$ LANGUAGE plpgsql;
B. CREATE FUNCTION f(OUT a INT) RETURNS RECORD AS $$ ... $$ LANGUAGE plpgsql;
C. CREATE FUNCTION f(a INT) RETURNS OUT INT AS $$ ... $$ LANGUAGE plpgsql;
D. CREATE FUNCTION f(a INT) RETURNS TABLE(OUT a INT) AS $$ ... $$ LANGUAGE plpgsql;

Solution

  1. Step 1: Check correct OUT parameter syntax

    OUT parameters are declared inside the parameter list as OUT name type. CREATE FUNCTION f(OUT a INT) RETURNS RECORD AS $$ ... $$ LANGUAGE plpgsql; matches this.
  2. Step 2: Analyze other options

    CREATE FUNCTION f(a INT OUT) RETURNS VOID AS $$ ... $$ LANGUAGE plpgsql; wrongly places OUT after type. CREATE FUNCTION f(a INT) RETURNS OUT INT AS $$ ... $$ LANGUAGE plpgsql; misuses RETURNS OUT. CREATE FUNCTION f(a INT) RETURNS TABLE(OUT a INT) AS $$ ... $$ LANGUAGE plpgsql; misuses RETURNS TABLE with OUT inside parentheses.
  3. Final Answer:

    CREATE FUNCTION f(OUT a INT) RETURNS RECORD AS $$ ... $$ LANGUAGE plpgsql; -> Option B
  4. Quick Check:

    OUT parameters declared as 'OUT name type' [OK]
Hint: OUT parameters go inside parentheses before RETURNS [OK]
Common Mistakes:
  • Placing OUT after the type
  • Using RETURNS OUT instead of parameter list
  • Confusing RETURNS TABLE syntax
3. Given the function:
CREATE FUNCTION get_person() RETURNS TABLE(id INT, name TEXT) AS $$
BEGIN
  RETURN QUERY SELECT 1, 'Alice';
END;
$$ LANGUAGE plpgsql;

What will be the output of SELECT * FROM get_person();?
medium
A. One row with columns id=1 and name='Alice'
B. No rows returned
C. Error: function does not have OUT parameters
D. One row with a single column containing a record

Solution

  1. Step 1: Understand RETURNS TABLE behavior

    RETURNS TABLE defines OUT parameters implicitly, so the function returns rows with columns id and name.
  2. Step 2: Analyze the RETURN QUERY statement

    The query returns one row with values (1, 'Alice'), so SELECT * FROM function returns that row.
  3. Final Answer:

    One row with columns id=1 and name='Alice' -> Option A
  4. Quick Check:

    RETURNS TABLE returns rows with named columns [OK]
Hint: RETURNS TABLE means function returns rows with named columns [OK]
Common Mistakes:
  • Thinking RETURNS TABLE needs explicit OUT keyword
  • Expecting no rows or error
  • Assuming single column record output
4. Consider this function definition:
CREATE FUNCTION calc_sum(a INT, b INT, OUT result INT) AS $$
BEGIN
  result := a + b;
END;
$$ LANGUAGE plpgsql;

Which of the following calls will cause an error?
medium
A. SELECT * FROM calc_sum(2, 3);
B. SELECT calc_sum(2, 3);
C. CALL calc_sum(2, 3, result_var);
D. SELECT result FROM calc_sum(2, 3);

Solution

  1. Step 1: Understand how OUT parameters affect function call

    Functions with OUT parameters return a record, so SELECT * FROM calc_sum(2, 3); works and returns result column.
  2. Step 2: Analyze each call

    SELECT calc_sum(2, 3); returns a record but as a single column, valid. SELECT result FROM calc_sum(2, 3); tries to select a column named 'result' directly from function call without FROM, which is invalid syntax. CALL calc_sum(2, 3, result_var); is invalid syntax for CALL with OUT parameters and will cause an error.
  3. Final Answer:

    CALL calc_sum(2, 3, result_var); -> Option C
  4. Quick Check:

    CALL with OUT parameters requires different syntax [OK]
Hint: Use SELECT * FROM function() with OUT params, not SELECT column directly or CALL with extra params [OK]
Common Mistakes:
  • Selecting OUT column without FROM clause
  • Using CALL incorrectly with OUT parameters
  • Assuming function returns scalar value
5. You want to create a function that returns both the length and uppercase version of a text input using OUT parameters. Which of the following function definitions correctly achieves this?
hard
A. CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql;
B. CREATE FUNCTION text_info(input TEXT) RETURNS TABLE(len INT, upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql;
C. CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) RETURNS RECORD AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql;
D. CREATE FUNCTION text_info(input TEXT) RETURNS RECORD AS $$ BEGIN RETURN QUERY SELECT length(input), upper(input); END; $$ LANGUAGE plpgsql;

Solution

  1. Step 1: Check OUT parameter declaration and assignment

    CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; declares OUT parameters in the signature and assigns values inside the function body, which is correct.
  2. Step 2: Compare other options

    CREATE FUNCTION text_info(input TEXT) RETURNS TABLE(len INT, upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; uses RETURNS TABLE but does not assign values properly. CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) RETURNS RECORD AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; mixes OUT parameters with RETURNS RECORD incorrectly. CREATE FUNCTION text_info(input TEXT) RETURNS RECORD AS $$ BEGIN RETURN QUERY SELECT length(input), upper(input); END; $$ LANGUAGE plpgsql; returns a record but does not use OUT parameters as requested.
  3. Final Answer:

    CREATE FUNCTION text_info(input TEXT, OUT len INT, OUT upper TEXT) AS $$ BEGIN len := length(input); upper := upper(input); END; $$ LANGUAGE plpgsql; -> Option A
  4. Quick Check:

    OUT params declared and assigned inside function [OK]
Hint: Declare OUT params in signature and assign inside function [OK]
Common Mistakes:
  • Mixing RETURNS TABLE with OUT params incorrectly
  • Not assigning OUT parameters inside function
  • Using RETURNS RECORD without OUT params