0
0
PostgreSQLquery~20 mins

OUT parameters in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.