Challenge - 5 Problems
Stored Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a simple stored function call
Consider the following MySQL stored function:
What is the output of the query
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;
Attempts:
2 left
💡 Hint
The function adds 5 to the input number.
✗ Incorrect
The function takes an integer input and returns the input plus 5. So, 10 + 5 = 15.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in stored function
Which option contains a syntax error when creating a stored function in MySQL?
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the function body.
✗ Incorrect
Option B is missing a semicolon after the RETURN statement, causing a syntax error.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Recursion can be slow and cause repeated work.
✗ Incorrect
Using a loop avoids the overhead of recursive calls and repeated calculations, making the function faster and more efficient.
🔧 Debug
advanced2:00remaining
Debugging a stored function that returns NULL unexpectedly
Given this stored function:
Why does
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;
Attempts:
2 left
💡 Hint
Think about how NULL behaves in comparisons.
✗ Incorrect
In MySQL, comparing NULL with any value results in NULL, which is treated as false in IF conditions, so the function returns NULL by default.
🧠 Conceptual
expert2:00remaining
Understanding deterministic vs nondeterministic stored functions
Which statement correctly describes a deterministic stored function in MySQL?
Attempts:
2 left
💡 Hint
Think about consistency of output for given inputs.
✗ Incorrect
Deterministic functions always produce the same output for the same inputs, which helps optimization and caching.