Challenge - 5 Problems
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple PostgreSQL function
What is the output of this PostgreSQL function call?
CREATE OR REPLACE FUNCTION add_five(x integer) RETURNS integer AS $$ BEGIN RETURN x + 5; END; $$ LANGUAGE plpgsql; SELECT add_five(10);
PostgreSQL
CREATE OR REPLACE FUNCTION add_five(x integer) RETURNS integer AS $$ BEGIN RETURN x + 5; END; $$ LANGUAGE plpgsql; SELECT add_five(10);
Attempts:
2 left
💡 Hint
The function adds 5 to the input number.
✗ Incorrect
The function add_five takes an integer input and returns the input plus 5. Calling add_five(10) returns 15.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in function creation
Which option contains a syntax error in the PostgreSQL function definition?
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the function body.
✗ Incorrect
Option B is missing a semicolon after RETURN x + y, which causes a syntax error.
❓ optimization
advanced2:00remaining
Choose the most efficient function for returning a constant value
Which function definition is the most efficient way to return the constant integer 42 in PostgreSQL?
Attempts:
2 left
💡 Hint
SQL language functions are simpler and faster for single expressions.
✗ Incorrect
Option D uses LANGUAGE sql with a simple SELECT statement, which is more efficient than plpgsql blocks for returning constants.
🧠 Conceptual
advanced2:00remaining
Understanding function volatility categories
Which volatility category should you assign to a PostgreSQL function that always returns the current timestamp?
Attempts:
2 left
💡 Hint
The function returns different results even with the same inputs.
✗ Incorrect
Functions returning current timestamp are VOLATILE because their output can change on each call.
🔧 Debug
expert2:00remaining
Diagnose the error in this function creation
What error will this PostgreSQL function raise when created?
CREATE FUNCTION faulty_func(x integer) RETURNS integer AS $$
BEGIN
IF x > 0 THEN
RETURN x;
ELSE
RETURN;
END IF;
END;
$$ LANGUAGE plpgsql;PostgreSQL
CREATE FUNCTION faulty_func(x integer) RETURNS integer AS $$ BEGIN IF x > 0 THEN RETURN x; ELSE RETURN; END IF; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
Check the RETURN statements and their values.
✗ Incorrect
The ELSE branch has RETURN with no value, but the function must return an integer, causing an error.