Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find maximum avg

Tags:

sql

oracle

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.

like image 634
bqui56 Avatar asked Nov 08 '11 13:11

bqui56


People also ask

How do you find minimum maximum and average?

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.

Which query will give you highest average salary of employee?

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.


2 Answers

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;
like image 147
king_nak Avatar answered Nov 13 '22 20:11

king_nak


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

like image 27
Mathews Avatar answered Nov 13 '22 18:11

Mathews