Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Returning tables and sets
Summary

Instruction

You're also allowed to specify the record as a type in the function definition (when returning SETOF record, the output columns are not named or typed), as shown in the example below:

CREATE FUNCTION get_employee(id)
RETURNS SETOF record AS $$
BEGIN
  RETURN QUERY
    SELECT id, name
    FROM employee AS e
    WHERE e.id = id;
END;
$$ LANGUAGE plpgsql;

And when calling the function, you need to provide a column definition list for functions returning a record:

SELECT *
FROM get_employee(10) AS (id INT, name VARCHAR);

Exercise

Create a new function named get_post_scores() that returns the upvotes and downvotes for each post. The function should return the SETOF of records that contain: the post ID, the upvotes, and the downvotes.

The function shouldn't take any arguments.