Highest Salary in each department

前端 未结 30 1446
没有蜡笔的小新
没有蜡笔的小新 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:09

    IF you want Department and highest salary, use

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

    if you want more columns in employee and department, use

    select  Department.Name , emp.Name, emp.Salary from Employee emp
    inner join (select DeptID, max(salary) [salary] from employee group by DeptID) b
    on emp.DeptID = b.DeptID and b.salary = emp.Salary
    inner join Department on emp.DeptID = Department.id
    order by Department.Name
    

    if use salary in (select max(salary...)) like this, one person have same salary in another department then it will fail.

提交回复
热议问题