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();
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
thanks for the fast reply
i didn't get an error message, but undesired output
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