Highest Salary in each department

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

    As short as the question:

    SELECT DeptID, MAX(Salary) FROM EmpDetails GROUP BY DeptID
    
    0 讨论(0)
  • 2021-01-30 22:59

    If you just want to get the highest salary from that table, by department:

    SELECT MAX(Salary) FROM TableName GROUP BY DeptID
    
    0 讨论(0)
  • 2021-01-30 23:01

    Use the below quesry:

    select employee_name,salary,department_id from emp where salary in(select max(salary) from emp group by department_id);
    
    0 讨论(0)
  • 2021-01-30 23:02
    ***
    
    > /*highest salary by each dept*/
    
    ***
    select d.Dept_Name,max(e.salary)
        from emp_details as e join Dept_Details as d
        on e.d_id=d.Dept_Id
        group by  d.Dept_Name
    
    select  distinct e.d_id,d.Dept_Name
        from emp_details as e join Dept_Details as d 
        on e.d_id=d.Dept_Id
    
    select  e.salary,d.Dept_Name,d.Dept_Id
        from emp_details as e join Dept_Details as d 
        on e.d_id=d.Dept_Id
    
    /////simplest query for max salary dept_wise////
    
    0 讨论(0)
  • 2021-01-30 23:04

    Not sure why no one has mentioned the Group By .... Having syntax. It specifically addresses these requirements.

    select EmpName,DeptId,Salary from EmpDetails group by DeptId having Salary=max(Salary);
    
    0 讨论(0)
  • 2021-01-30 23:05
    Select empname,empid,Sal,DeptName from 
    (Select e.empname,e.empid,Max(S.Salary) Sal,D.DeptName, ROW_NUMBER() Over(partition by D.DeptName order by s.salary desc) Rownum
    from emp e inner join Sal S
    on e.empid=s.empid 
    inner join Dept d on e.Deptid=d.Deptid
    group by e.empname,e.empid,D.DeptName,s.Salary
    ) x where Rownum = 1
    
    0 讨论(0)
提交回复
热议问题