0
0
SQLquery~20 mins

Function vs procedure decision in SQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function vs Procedure Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use a function instead of a procedure?
Which scenario best fits the use of a SQL function rather than a procedure?
AYou want to run a batch job that sends emails and logs results.
BYou want to perform multiple data modifications without returning a value.
CYou need to return a single value that can be used in a SELECT statement.
DYou need to execute a series of commands that include transaction control.
Attempts:
2 left
💡 Hint
Functions return values and can be used inside queries.
query_result
intermediate
2: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;
A0.1
B10.0
C100.0
DSyntax error
Attempts:
2 left
💡 Hint
The function returns 10% of the amount.
📝 Syntax
advanced
2: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;
ACREATE PROCEDURE update_salary(emp_id INT, new_salary DECIMAL) AS BEGIN UPDATE employees SET salary = new_salary WHERE id = emp_id; END;
BCREATE PROCEDURE update_salary(emp_id INT, new_salary DECIMAL) BEGIN UPDATE employees SET salary = new_salary WHERE id = emp_id; END;
CCREATE PROCEDURE update_salary(emp_id INT, new_salary DECIMAL) BEGIN UPDATE employees SET salary = new_salary WHERE id = emp_id END;
D;DNE ;di_pme = di EREHW yralas_wen = yralas TES seeyolpme ETADPU NIGEB )LAMICED yralas_wen ,TNI di_pme(yralas_etadpu ERUDECORP ETAERC
Attempts:
2 left
💡 Hint
Check the keywords used in procedure declaration.
optimization
advanced
2: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?
AUse a procedure because it can handle multiple statements efficiently.
BUse a function because it can modify data during the SELECT.
CUse a procedure because it avoids recalculating values.
DUse a function because it can be inlined and called within the SELECT.
Attempts:
2 left
💡 Hint
Functions can be called inside queries and sometimes optimized by the database engine.
🔧 Debug
expert
3:00remaining
Why does this function cause an error when called?
Consider this function definition and call:

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);?
AFunction does not return a value error
BSyntax error in function body
CType mismatch error
DNo error, returns employee name
Attempts:
2 left
💡 Hint
Functions must explicitly return a value.