Which of the following best explains why utility functions are important in SQL databases?
Think about how repeating the same code in many places can be avoided.
Utility functions let you write a piece of logic once and use it many times, making queries easier to read and maintain.
Given this utility function in PostgreSQL:
CREATE OR REPLACE FUNCTION add_tax(price numeric) RETURNS numeric AS $$ BEGIN RETURN price * 1.1; END; $$ LANGUAGE plpgsql;
What is the result of SELECT add_tax(100);?
The function adds 10% tax to the price.
The function multiplies the input by 1.1, so 100 becomes 110.
Which option contains the correct syntax for a PostgreSQL utility function that returns the square of an integer?
CREATE OR REPLACE FUNCTION square_num(n integer) RETURNS integer AS $$ BEGIN RETURN n * n; END; $$ LANGUAGE plpgsql;
Check for missing semicolons and correct use of language declaration.
Option B correctly includes semicolons and language declaration. Others miss semicolons or language keyword.
Which statement best describes how utility functions can improve performance in PostgreSQL?
Think about what happens when a function is called multiple times in queries.
Utility functions are compiled once and reused, which can reduce overhead in query execution.
Consider this PostgreSQL function:
CREATE OR REPLACE FUNCTION get_discounted_price(price numeric, discount_percent numeric) RETURNS numeric AS $$ BEGIN RETURN price - price * discount_percent / 100; END; $$ LANGUAGE plpgsql;
When calling SELECT get_discounted_price(200, 20);, the result is 160. But the expected result is 180. What is the most likely cause?
Calculate 20% of 200 and subtract it from 200.
20% of 200 is 40, so 200 - 40 = 160. The function works correctly; the expected result is incorrect.