Highest Salary in each department

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

I have a table EmpDetails:

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


        
相关标签:
30条回答
  • SELECT empName,empDept,EmpSalary
    FROM Employee
    WHERE empSalary IN
      (SELECT max(empSalary) AS salary
       From Employee
       GROUP BY EmpDept)
    
    0 讨论(0)
  • 2021-01-30 22:52
    SELECT Employee_ID
         , First_name
         , last_name
         , department_id
         , Salary 
    FROM (SELECT Employee_ID
               , First_name
               , last_name
               , department_id
               , Salary
               , MAX(salary) OVER (PARTITION BY department_id) dept_max_sal
          FROM EMPLOYEES) AS Emp
    WHERE salary = dept_max_sal;
    
    0 讨论(0)
  • 2021-01-30 22:52

    Use following command;

    SELECT  A.*
        FROM    @EmpDetails A
                INNER JOIN ( SELECT DeptID ,
                                    MAX(salary) AS salary
                             FROM   @EmpDetails
                             GROUP BY DeptID
                           ) B ON A.DeptID = B.DeptID
                                  AND A.salary = B.salary
        ORDER BY A.DeptID
    
    0 讨论(0)
  • 2021-01-30 22:53
    select * from (
        select a.* from EmpDetails a 
        right join (select DeptID,max(salary) as Salary from EmpDetails group by DeptID) b
        on b.DeptID=a.DeptID and b.salary=a.salary ) as c  group by c.DeptID;
    
    0 讨论(0)
  • 2021-01-30 22:54
    SELECT empname
    FROM empdetails
    WHERE salary IN(SELECT deptid max(salary) AS salary
    FROM empdetails
    group by deptid)
    
    0 讨论(0)
  • 2021-01-30 22:55

    Assuming SQL Server 2005+

    WITH cteRowNum AS (
        SELECT DeptID, EmpName, Salary,
               DENSE_RANK() OVER(PARTITION BY DeptID ORDER BY Salary DESC) AS RowNum
            FROM EmpDetails
    )
    SELECT DeptID, EmpName, Salary
        FROM cteRowNum
        WHERE RowNum = 1;
    
    0 讨论(0)
提交回复
热议问题