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:
Here is how to also get the worker_id, inspired by previous answers:
SELECT worker_id, MAX(avg_salary)
FROM (SELECT worker_id, AVG(salary) AS avg_salary
FROM workers
GROUP BY worker_id);
You can in this way that the first row is sorted in descending based on average find
select top 1 worker_id, avg(salary) as avgsalary
from workers
group by worker_id
order by avgsalary desc
You can fix the query by adding a column alias to the column within the sub-query, like so:
select max(avg_salary)
from (select worker_id, avg(salary) avg_salary
from workers
group by worker_id);
However, if worker_id
uniquely identifies records on the workers table, this is functionally equivalent to (can be simplified to):
select max(salary) from workers;