0
0
MySQLquery~20 mins

Creating stored procedures in MySQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stored Procedure 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 procedure call
Consider the following stored procedure in MySQL:

DELIMITER $$
CREATE PROCEDURE GetEmployeeCount()
BEGIN
  SELECT COUNT(*) AS TotalEmployees FROM employees;
END$$
DELIMITER ;

What will be the output when you execute CALL GetEmployeeCount(); assuming the employees table has 50 rows?
MySQL
CALL GetEmployeeCount();
AA single row with column TotalEmployees and value 50
BNo output, only a success message
CAn error because stored procedures cannot return SELECT results
DA list of all employee names
Attempts:
2 left
💡 Hint
Stored procedures can return result sets using SELECT statements.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in stored procedure creation
Which option contains a syntax error when creating a stored procedure in MySQL?
MySQL
CREATE PROCEDURE SampleProc()
BEGIN
  SELECT * FROM users;
END;
ACREATE PROCEDURE SampleProc() BEGIN SELECT * FROM users END;
BCREATE PROCEDURE SampleProc() BEGIN SELECT * FROM users; END;
CCREATE PROCEDURE SampleProc() BEGIN SELECT * FROM users; END
DCREATE PROCEDURE SampleProc() BEGIN SELECT * FROM users; END;;
Attempts:
2 left
💡 Hint
Check for missing semicolons inside the procedure body.
optimization
advanced
2:00remaining
Optimizing a stored procedure with parameters
You have a stored procedure that takes a department ID and returns employees in that department:

CREATE PROCEDURE GetEmployeesByDept(IN dept_id INT)
BEGIN
  SELECT * FROM employees WHERE department_id = dept_id;
END;

Which option improves this procedure to avoid confusion between parameter and column names?
AUse <code>WHERE department_id = dept_id</code> as is
BUse <code>WHERE department_id = employees.dept_id</code>
CUse <code>WHERE department_id = GetEmployeesByDept.dept_id</code>
DUse <code>WHERE department_id = IN_dept_id</code> and rename parameter to IN_dept_id
Attempts:
2 left
💡 Hint
Parameter names can conflict with column names; renaming helps clarity.
🔧 Debug
advanced
2:00remaining
Debugging a stored procedure with variable assignment
What error will occur when running this stored procedure?

CREATE PROCEDURE CalcTotal()
BEGIN
  DECLARE total INT;
  SET total = SELECT COUNT(*) FROM orders;
  SELECT total;
END;
ANo error, returns total count of orders
BSyntax error near 'SELECT COUNT(*) FROM orders;'
CRuntime error: variable 'total' not declared
DError: SELECT statement cannot be used in SET
Attempts:
2 left
💡 Hint
Check the syntax for assigning query results to variables.
🧠 Conceptual
expert
2:00remaining
Understanding transaction control in stored procedures
Which statement about transaction control inside MySQL stored procedures is correct?
AYou cannot use transaction control statements inside stored procedures at all.
BStored procedures automatically commit after each statement; explicit COMMIT is not allowed.
CYou can use COMMIT and ROLLBACK inside stored procedures to control transactions.
DTransactions inside stored procedures are controlled only by the client application, not the procedure.
Attempts:
2 left
💡 Hint
Think about how transactions work in MySQL and stored procedures.