Last answered:

10 May 2023

Posted on:

04 May 2023

0

why is this code not valid

use employees;
Delimiter $$
create procedure average_salary
BEGIN 
          SELECT AVG(total_salary)
          FROM (SELECT d.emp_no, SUM(s.salary) AS total_salary
          FROM dept_manager d
          JOIN salaries s ON d.emp_no = s.emp_no
           GROUP BY d.emp_no) ;
END $$
delimiter ;

2 answers ( 0 marked as helpful)
Posted on:

05 May 2023

0

You cannot aggregate the average salary from a sum of the salaries combined.

Instructor
Posted on:

10 May 2023

0

Hi Abdelrahman!
Thanks for reaching out.


As Ronith said you cannot use the average salary from a sum of the salaries combined. Please, use the following code:

DELIMITER$$
CREATE PROCEDURE average_salary()
BEGIN 
          SELECT AVG(salary) 
          FROM 
          dept_manager d
          JOIN salaries s ON d.emp_no = s.emp_no
          GROUP BY d.emp_no;
END $$
DELIMITER;



Hope this helps.
Best,
Tsvetelin

Submit an answer