Home > database >  How do I get the Average of maximum value?
How do I get the Average of maximum value?

Time:05-03

I have a SQL server problem where i have to get the average of the maximum salaries in each department. For example I have a department with id 1 that has an employee with a maximum salary of 50000, and a department with id 2 that has an employee with a maximum salary of 30000, what i have to do is calculate the average between these 2. What I tried:

SELECT AVG(MAX(salary)) 
  FROM employees 
 GROUP BY department_id

CodePudding user response:

most simple think you can do is (if in the employee table, you have a department_id) :

SELECT AVG(p.maximum) 
  FROM (SELECT department_id, MAX(salary) AS maximum 
          FROM employees 
         GROUP BY department_id) p
  • Related