0

I am trying to get the maximum average salary and i am using this:

select max (avg_salary)
from (select name, avg(salary) AS avg_salary
  from employee
  group by name);

But is another way to get same result without using subquery?

Martin C
  • 395
  • 1
  • 7
  • 21

5 Answers5

2
SELECT AVG(salary)
FROM employee
GROUP BY name
ORDER BY AVG(salary) DESC
LIMIT 1
fthiella
  • 48,073
  • 15
  • 90
  • 106
1

One other option would be:

SELECT name, avg(salary) AS avg_salary
FROM employee
GROUP BY name
ORDER BY 2 DESC LIMIT 1;
Filipe Silva
  • 21,189
  • 5
  • 53
  • 68
0

Use this instead

SELECT name, 
       avg(salary) AS avg_salary   
FROM employee 
GROUP BY name 
ORDER BY DESC avg_salary 
      LIMIT 1
Hawk
  • 5,060
  • 12
  • 49
  • 74
M Shahzad Khan
  • 935
  • 1
  • 9
  • 22
0

Try this... this sort desc of avg salary then apply limit

SELECT AVG(salary),name
FROM employee GROUP BY name
ORDER BY AVG(salary) DESC
LIMIT 1
naveen goyal
  • 4,571
  • 2
  • 16
  • 26
0

All employees:

SELECT name, 
       AVG(salary) AS avg_salary 
FROM employee 
GROUP BY name 
ORDER BY avg_salary DESC

Maximum of one employee:

SELECT name, 
AVG(salary) AS avg_salary 
FROM employee 
GROUP BY name 
ORDER BY avg_salary DESC 
LIMIT 1
Hawk
  • 5,060
  • 12
  • 49
  • 74