books table with columns id, title, and price.get_books_above_price that takes a min_price parameter and returns SETOF books.RETURN QUERY statement inside the function to select books with price greater than or equal to min_price.Jump into concepts and practice - no test required
books table with columns id, title, and price.get_books_above_price that takes a min_price parameter and returns SETOF books.RETURN QUERY statement inside the function to select books with price greater than or equal to min_price.books table and insert databooks with columns id as integer primary key, title as text, and price as numeric. Then insert these three rows exactly: (1, 'The Hobbit', 15.99), (2, '1984', 12.50), (3, 'Clean Code', 33.00).Use CREATE TABLE to define the table and INSERT INTO to add rows.
get_books_above_price that takes a parameter min_price of type numeric and returns SETOF books. Use LANGUAGE plpgsql and start the function body with BEGIN.Use CREATE FUNCTION with the correct parameter and return type, and start the function with BEGIN.
RETURN QUERY statement that selects all columns from books where price >= min_price. Keep the BEGIN and END; keywords.Use RETURN QUERY SELECT * FROM books WHERE price >= min_price; inside the function.
get_books_above_price with the argument 15 to retrieve all books priced 15 or more.Use SELECT * FROM get_books_above_price(15); to call the function.
What does a PostgreSQL function declared with RETURNS SETOF do?
RETURNS SETOF means the function returns multiple rows, not just one value.Which of the following is the correct syntax to declare a PostgreSQL function that returns a set of integers?
CREATE FUNCTION get_numbers() RETURNS SETOF integer AS $$ BEGIN RETURN QUERY SELECT ...; END; $$ LANGUAGE plpgsql;
RETURNS SETOF integer.RETURN QUERY SELECT ... returns multiple rows properly.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;
SELECT * FROM get_even_numbers(); return?WHERE generate_series % 2 = 0.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;
RETURN QUERY SELECT ... inside the function.RETURN SELECT ... which is invalid; it must be RETURN QUERY SELECT ....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?
SELECT * FROM function_name(args); to get all rows returned by the function.