Bird
Raised Fist0
PostgreSQLquery~20 mins

Why utility functions matter in PostgreSQL - Challenge Your Understanding

Choose your learning style10 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.

Practice

(1/5)
1. Why are utility functions important in PostgreSQL databases?
easy
A. They help reuse code and simplify queries
B. They slow down query execution significantly
C. They make the database schema more complex
D. They prevent any data from being updated

Solution

  1. Step 1: Understand the purpose of utility functions

    Utility functions are designed to perform common tasks repeatedly without rewriting code.
  2. Step 2: Recognize benefits in query simplification

    By using utility functions, queries become simpler and easier to maintain because logic is centralized.
  3. Final Answer:

    They help reuse code and simplify queries -> Option A
  4. Quick Check:

    Utility functions = reuse and simplify [OK]
Hint: Utility functions = reuse code + simplify queries [OK]
Common Mistakes:
  • Thinking utility functions slow down queries
  • Believing they complicate the database schema
  • Assuming they block data updates
2. Which of the following is the correct syntax to create a simple utility function in PostgreSQL that returns the square of an integer?
easy
A. CREATE FUNCTION square(x INT) RETURNS INT AS 'RETURN x * x;' LANGUAGE sql;
B. CREATE FUNCTION square(x INT) RETURNS INT BEGIN RETURN x * x; END;
C. CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql;
D. CREATE FUNCTION square(x INT) RETURNS INT AS $$ RETURN x * x; $$ LANGUAGE plpgsql;

Solution

  1. Step 1: Identify correct function creation syntax

    PostgreSQL requires the function body inside $$ with BEGIN and END for plpgsql language.
  2. Step 2: Check for proper RETURNS and LANGUAGE clauses

    CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; correctly uses RETURNS INT, BEGIN...END block, and LANGUAGE plpgsql.
  3. Final Answer:

    CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; -> Option C
  4. Quick Check:

    Correct plpgsql function syntax = CREATE FUNCTION square(x INT) RETURNS INT AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql; [OK]
Hint: Use $$ with BEGIN...END and specify LANGUAGE plpgsql [OK]
Common Mistakes:
  • Omitting BEGIN and END in plpgsql functions
  • Missing LANGUAGE clause
  • Incorrect function body syntax
3. Given the function below, what will be the output of SELECT multiply_by_two(5);?
CREATE FUNCTION multiply_by_two(n INT) RETURNS INT AS $$ BEGIN RETURN n * 2; END; $$ LANGUAGE plpgsql;
medium
A. 5
B. NULL
C. Error: function not found
D. 10

Solution

  1. Step 1: Understand the function logic

    The function takes an integer n and returns n multiplied by 2.
  2. Step 2: Apply the input value 5

    Calling multiply_by_two(5) returns 5 * 2 = 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Multiply input by 2 as function defines [OK]
Common Mistakes:
  • Confusing input and output values
  • Assuming function returns input unchanged
  • Expecting NULL or error without reason
4. You wrote this utility function but get an error when calling it:
CREATE FUNCTION add_five(n INT) RETURNS INT AS $$ BEGIN RETURN n + 5 END; $$ LANGUAGE plpgsql;
What is the likely cause of the error?
medium
A. Wrong data type for parameter
B. Missing semicolon after RETURN statement
C. Incorrect function name
D. LANGUAGE clause is missing

Solution

  1. Step 1: Check function syntax carefully

    In plpgsql, each statement must end with a semicolon; the RETURN line lacks one.
  2. Step 2: Identify missing semicolon causing syntax error

    Adding a semicolon after RETURN n + 5 fixes the error.
  3. Final Answer:

    Missing semicolon after RETURN statement -> Option B
  4. Quick Check:

    Semicolon needed after RETURN [OK]
Hint: Always end statements with semicolon in plpgsql [OK]
Common Mistakes:
  • Forgetting semicolon after RETURN
  • Assuming function name causes error
  • Ignoring LANGUAGE clause presence
5. You want to create a utility function that returns the length of a text string but returns 0 if the input is NULL. Which function definition correctly implements this behavior?
hard
A. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql;
B. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN RETURN COALESCE(s, 0); END; $$ LANGUAGE plpgsql;
C. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ RETURN LENGTH(s) OR 0; $$ LANGUAGE sql;
D. CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN RETURN LENGTH(s); END; $$ LANGUAGE plpgsql;

Solution

  1. Step 1: Understand requirement for NULL input handling

    The function must return 0 if input is NULL, else return string length.
  2. Step 2: Analyze each option for NULL safety

    CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql; explicitly checks for NULL and returns 0, fulfilling the requirement correctly.
  3. Final Answer:

    CREATE FUNCTION safe_length(s TEXT) RETURNS INT AS $$ BEGIN IF s IS NULL THEN RETURN 0; ELSE RETURN LENGTH(s); END IF; END; $$ LANGUAGE plpgsql; -> Option A
  4. Quick Check:

    Explicit NULL check returns 0 [OK]
Hint: Use IF NULL THEN return 0 ELSE length END IF [OK]
Common Mistakes:
  • Not handling NULL input explicitly
  • Using COALESCE incorrectly with LENGTH
  • Assuming LENGTH returns 0 for NULL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard