Challenge - 5 Problems
Function vs Procedure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When to use a function instead of a procedure?
Which scenario best fits the use of a SQL function rather than a procedure?
Attempts:
2 left
💡 Hint
Functions return values and can be used inside queries.
✗ Incorrect
Functions are designed to return a value and can be called within SQL statements like SELECT. Procedures are better for performing actions without returning values or for complex operations.
❓ query_result
intermediate2:00remaining
Output of calling a function in a SELECT
Given the function below, what is the output of the query
SELECT calculate_tax(100);?SQL
CREATE FUNCTION calculate_tax(amount DECIMAL) RETURNS DECIMAL DETERMINISTIC BEGIN RETURN amount * 0.1; END;
Attempts:
2 left
💡 Hint
The function returns 10% of the amount.
✗ Incorrect
The function multiplies the input by 0.1, so 100 * 0.1 = 10.0.
📝 Syntax
advanced2:00remaining
Identify the syntax error in procedure declaration
Which option contains a syntax error in the procedure declaration?
SQL
CREATE PROCEDURE update_salary(emp_id INT, new_salary DECIMAL) BEGIN UPDATE employees SET salary = new_salary WHERE id = emp_id; END;
Attempts:
2 left
💡 Hint
Check the keywords used in procedure declaration.
✗ Incorrect
The keyword 'AS' is not used in standard SQL procedure declarations; it causes a syntax error here.
❓ optimization
advanced2:00remaining
Choosing between function and procedure for performance
You need to perform a calculation repeatedly inside a large SELECT query. Which choice is best for performance?
Attempts:
2 left
💡 Hint
Functions can be called inside queries and sometimes optimized by the database engine.
✗ Incorrect
Functions are designed to be called within queries and can be optimized or inlined, improving performance for repeated calculations.
🔧 Debug
expert3:00remaining
Why does this function cause an error when called?
Consider this function definition and call:
What error occurs when running
CREATE FUNCTION get_employee_name(emp_id INT) RETURNS VARCHAR(100) DETERMINISTIC BEGIN
SELECT name FROM employees WHERE id = emp_id;
END;What error occurs when running
SELECT get_employee_name(5);?Attempts:
2 left
💡 Hint
Functions must explicitly return a value.
✗ Incorrect
The function uses SELECT but does not assign or RETURN the value explicitly, causing an error about missing return value.