Highest Salary in each department

前端 未结 30 1448
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 22:16

I have a table EmpDetails:

DeptID      EmpName   Salary
Engg        Sam       1000
Engg        Smith     2000
HR          Denis     1500
HR                  


        
30条回答
  •  伪装坚强ぢ
    2021-01-30 23:11

    SELECT DeptID, MAX(Salary)
     FROM EmpDetails
    GROUP BY DeptID
    

    This query will work fine, but the moment if you want to fetch some others details related to the employee having the highest salary will contradict. You can use :

    SELECT DepatID, a , b, c
     FROM EmpDetails
     WHERE Salary IN (
        SELECT max(Salary)
          FROM EmpDetails
         GROUP BY DeptID
     );
    

    if you will use the previous query it will only reflects the records of the min val except the salary as you have used the max function.

提交回复
热议问题