I am trying to display the maximum average salary; however, I can't seem to get it to work.
I can get a list of the average salaries to display with:
select worker_id, avg(salary)
from workers
group by worker_id;
However, when I try to display a list of the maximum average salary with:
select max (avg(salary))
from (select worker_id, avg(salary)
from workers
group by worker_id);
it doesn't run. I get an "invalid identifier" error. How do I use the average salary for each worker to find the maximum average for each worker?
Thanks.
Keep a minimum variable that is initialized to a high value, and update it if you see a lower value. Do the opposite with a maximum variable. Add up all numbers and divide that sum by the total count to get the average.
One such data is the name of the department having the highest average salary of employees working in it. We shall use the TOP, AVG, ORDER BY, AS, GROUP BY, and DESC clauses to achieve this.
Columns resulting from aggregate functions (e.g. avg) usually get arbitrary names. Just use an alias for it, and select on that:
select max(avg_salary)
from (select worker_id, avg(salary) AS avg_salary
from workers
group by worker_id) As maxSalary;
select worker_id, avgsal
from
(
select worker_id, avg(salary) as avgsal
from workers
group by worker_id
)
where avgsal=(select max(avgsal)
from (select worker_id, avg(salary) as avgsal
from workers group by worker_id))
This will display the highest average along with worker id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With