0
0
PostgreSQLquery~20 mins

Function creation syntax in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A15
BError: function does not exist
C5
D10
Attempts:
2 left
💡 Hint
The function adds 5 to the input number.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in function creation
Which option contains a syntax error in the PostgreSQL function definition?
ACREATE FUNCTION subtract(x integer, y integer) RETURNS integer AS $$ BEGIN RETURN x - y; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION add(x integer, y integer) RETURNS integer AS $$ BEGIN RETURN x + y; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION greet() RETURNS text AS $$ BEGIN RETURN 'Hello'; END $$ LANGUAGE plpgsql;
DCREATE FUNCTION multiply_by_two(x integer) RETURNS integer AS $$ BEGIN RETURN x * 2; END; $$ LANGUAGE plpgsql;
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the function body.
optimization
advanced
2: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?
ACREATE FUNCTION get_constant() RETURNS integer AS $$ BEGIN RETURN 40 + 2; END; $$ LANGUAGE plpgsql;
BCREATE FUNCTION get_constant() RETURNS integer AS $$ BEGIN RETURN 42; END; $$ LANGUAGE plpgsql;
CCREATE FUNCTION get_constant() RETURNS integer AS $$ BEGIN RETURN CAST('42' AS integer); END; $$ LANGUAGE plpgsql;
DCREATE FUNCTION get_constant() RETURNS integer AS $$ SELECT 42; $$ LANGUAGE sql;
Attempts:
2 left
💡 Hint
SQL language functions are simpler and faster for single expressions.
🧠 Conceptual
advanced
2:00remaining
Understanding function volatility categories
Which volatility category should you assign to a PostgreSQL function that always returns the current timestamp?
AVOLATILE
BIMMUTABLE
CSTABLE
DCONSTANT
Attempts:
2 left
💡 Hint
The function returns different results even with the same inputs.
🔧 Debug
expert
2: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;
ASyntax error: missing semicolon
BRuntime error: function returns NULL without explicit return type
CError: RETURN statement with no value in function returning integer
DNo error, function runs correctly
Attempts:
2 left
💡 Hint
Check the RETURN statements and their values.