Retrieve Average Salary Using a MySQL Stored Procedure
Call the Stored Procedure to Calculate Average Salary
Write a MySQL query to call the CalculateAverageSalary stored procedure and retrieve the average salary.
Solution:
-- Declare a variable to store the average salary
SET @avg_salary = 0;
-- Call the `CalculateAverageSalary` stored procedure
CALL CalculateAverageSalary(2, @avg_salary);
-- Retrieve the average salary
SELECT @avg_salary AS AverageSalary;
Explanation:
- Purpose of the Query:
- The goal is to execute the stored procedure and retrieve the average salary.
- Key Components:
- CALL: Executes the stored procedure.
- SELECT @avg_salary: Retrieves the output parameter.
- Why use Stored Procedures?:
- Stored procedures simplify complex operations and improve code reusability
- Real-World Application:
- For example, in a payroll system, you might call a stored procedure to calculate department averages.
For more Practice: Solve these Related Problems:
- Write a MySQL query to call a stored procedure that calculates the average budget of departments in the Departments table.
- Write a MySQL query to call a stored procedure that calculates the average duration of projects in the Projects table.
- Write a MySQL query to call a stored procedure that calculates the average age of customers in the Customers table.
- Write a MySQL query to call a stored procedure that calculates the average total amount of orders in the Orders table.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous MySQL Exercise: Create a Stored Procedure to Calculate Average Salary.
Next MySQL Exercise: Create a Trigger to Update Last Modified Date.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics