0
0
PostgreSQLquery~20 mins

Why utility functions matter in PostgreSQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Utility Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use utility functions in SQL?

Which of the following best explains why utility functions are important in SQL databases?

AThey help reuse code and simplify complex queries by encapsulating logic.
BThey automatically optimize the database storage space.
CThey replace the need for indexes on tables.
DThey allow direct editing of database files outside SQL.
Attempts:
2 left
💡 Hint

Think about how repeating the same code in many places can be avoided.

query_result
intermediate
2:00remaining
Output of a utility function call

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);?

AError: function does not exist
B100.0
C10.0
D110.0
Attempts:
2 left
💡 Hint

The function adds 10% tax to the price.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this utility function

Which option contains the correct syntax for a PostgreSQL utility function that returns the square of an integer?

PostgreSQL
CREATE OR REPLACE FUNCTION square_num(n integer) RETURNS integer AS $$
BEGIN
  RETURN n * n;
END;
$$ LANGUAGE plpgsql;
ACREATE OR REPLACE FUNCTION square_num(n integer) RETURNS integer AS $$ BEGIN RETURN n * n; END $$ LANGUAGE plpgsql;
BCREATE OR REPLACE FUNCTION square_num(n integer) RETURNS integer AS $$ BEGIN RETURN n * n; END; $$ LANGUAGE plpgsql;
CCREATE OR REPLACE FUNCTION square_num(n integer) RETURNS integer AS $$ BEGIN RETURN n * n; END; $$ LANGUAGE plpgsql
DCREATE FUNCTION square_num(n integer) RETURNS integer AS $$ BEGIN RETURN n * n END $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint

Check for missing semicolons and correct use of language declaration.

optimization
advanced
2:00remaining
How utility functions improve query performance

Which statement best describes how utility functions can improve performance in PostgreSQL?

AThey automatically create indexes on columns used inside the function.
BThey store query results permanently to speed up future queries.
CThey allow complex logic to be precompiled and reused, reducing parsing and planning time.
DThey replace the need for database normalization.
Attempts:
2 left
💡 Hint

Think about what happens when a function is called multiple times in queries.

🔧 Debug
expert
3:00remaining
Debugging a utility function with unexpected output

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?

AThe function applies the discount correctly; the expected result is wrong.
BThe discount_percent should be divided by 10, not 100.
CThe function should add the discount instead of subtracting it.
DThe discount_percent argument is being treated as a whole number instead of a decimal.
Attempts:
2 left
💡 Hint

Calculate 20% of 200 and subtract it from 200.