0
0
MySQLquery~20 mins

Creating stored functions in MySQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stored Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a simple stored function call
Consider the following MySQL stored function:

CREATE FUNCTION add_five(x INT) RETURNS INT
BEGIN
RETURN x + 5;
END;

What is the output of the query SELECT add_five(10);?
MySQL
CREATE FUNCTION add_five(x INT) RETURNS INT
BEGIN
  RETURN x + 5;
END;
A10
BNULL
C5
D15
Attempts:
2 left
💡 Hint
The function adds 5 to the input number.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in stored function
Which option contains a syntax error when creating a stored function in MySQL?
ACREATE FUNCTION multiply_by_two(x INT) RETURNS INT BEGIN RETURN x * 2; END
BCREATE FUNCTION multiply_by_two(x INT) RETURNS INT BEGIN RETURN x * 2 END;
CCREATE FUNCTION multiply_by_two(x INT) RETURNS INT BEGIN RETURN x * 2; END;
DCREATE FUNCTION multiply_by_two(x INT) RETURNS INT BEGIN RETURN x * 2; END;;
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the function body.
optimization
advanced
2:00remaining
Optimizing a stored function for repeated calculations
You have a stored function that calculates the factorial of a number recursively. Which option is the best way to optimize it to avoid repeated calculations?
AUse a loop inside the function instead of recursion.
BCall the function recursively without any changes.
CStore intermediate results in a temporary table inside the function.
DUse a global variable to store the last factorial calculated.
Attempts:
2 left
💡 Hint
Recursion can be slow and cause repeated work.
🔧 Debug
advanced
2:00remaining
Debugging a stored function that returns NULL unexpectedly
Given this stored function:

CREATE FUNCTION get_discount(price DECIMAL(10,2)) RETURNS DECIMAL(10,2)
BEGIN
IF price > 100 THEN
RETURN price * 0.1;
END IF;
RETURN 0;
END;

Why does SELECT get_discount(NULL); return NULL instead of 0?
MySQL
CREATE FUNCTION get_discount(price DECIMAL(10,2)) RETURNS DECIMAL(10,2)
BEGIN
  IF price > 100 THEN
    RETURN price * 0.1;
  END IF;
  RETURN 0;
END;
ABecause any comparison with NULL returns NULL, the IF condition is not true or false, so no RETURN is executed.
BBecause the function does not handle NULL inputs explicitly, it returns 0 by default.
CBecause MySQL automatically converts NULL to 0 in arithmetic operations.
DBecause the ELSE branch returns NULL explicitly.
Attempts:
2 left
💡 Hint
Think about how NULL behaves in comparisons.
🧠 Conceptual
expert
2:00remaining
Understanding deterministic vs nondeterministic stored functions
Which statement correctly describes a deterministic stored function in MySQL?
AA deterministic function cannot use any SQL statements inside its body.
BA deterministic function can return different results for the same input depending on server state.
CA deterministic function always returns the same result for the same input values.
DA deterministic function must not contain any RETURN statements.
Attempts:
2 left
💡 Hint
Think about consistency of output for given inputs.