Department Top Three Salaries

表内自连接,得到rank前三的记录
https://leetcode.com/problems/department-top-three-salaries/

SELECT D.Name AS Department,A.Employee,A.Salary FROM (SELECT C.DepartmentId,C.Name AS Employee,C.Salary FROM (SELECT E1.id,E1.Name,E1.Salary,E1.DepartmentId,COUNT(1) AS RANK FROM Employee E1 INNER JOIN (SELECT DISTINCT Salary,DepartmentId FROM Employee) E2 ON E1.DepartmentId=E2.DepartmentId AND E1.Salary<=E2.Salary GROUP BY E1.id,E1.Name,E1.Salary,E1.DepartmentId) C WHERE C.RANK<4) A INNER JOIN Department D ON D.Id=A.DepartmentId

你可能感兴趣的:(Department Top Three Salaries)