Last answered:

16 Dec 2021

Posted on:

29 Nov 2021

0

I cant find the error in my code

i can't get the average salary after running the query... help me please spot the error in the code

delimiter $$
create procedure average_salary()
    begin
    select AVG(salary) from salaries;
    end$$
delimiter ;
call average_salary();

3 answers ( 0 marked as helpful)
Instructor
Posted on:

29 Nov 2021

1

Hi Asmaa!
Great to have you in the course and thanks for reaching out!
Can you please support your question with a screenshot of the entire code you’ve executed and the error message you’ve encountered? This can help us assist you better. Thank you.
Looking forward to your answer.
Best,
Ivan

Posted on:

29 Nov 2021

0

thanks for the fast reply
i didn't get an error message, but undesired output
image.png

Instructor
Posted on:

16 Dec 2021

1

Hi Asmaa!
Thanks for reaching out!

Actually, your result set is obtained from the following code:

delimiter $$
create procedure average_salary()
    begin
    select emp_no, AVG(salary) from salaries
    group by emp_no;
    end$$
delimiter ;

To obtain the average salary for all employees you should use:

delimiter $$
create procedure average_salary()
begin

select AVG(salary) from salaries;
end$$
delimiter ;
call average_salary();
Some differences of the average salaries per each employee may occur because during different exercises some of the records are deleted. So, the idea is that if you want to obtain the average salaries per employee, you should use the GROUP BY clause, if you want to obtain the average salary for all employees, then you should use only the AVG() aggregate function applied to the salary column like this – AVG(salary).

Hope this helps.
Best,
Tsvetelin,
The 365 Team

Submit an answer