問題:spa
The Employee
table holds all employees. Every employee has an Id, and there is also a column for the department Id..net
+----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | | 5 | Janet | 69000 | 1 | | 6 | Randy | 85000 | 1 | +----+-------+--------+--------------+
The Department
table holds all departments of the company.code
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.排序
+------------+----------+--------+ | Department | Employee | Salary | +------------+----------+--------+ | IT | Max | 90000 | | IT | Randy | 85000 | | IT | Joe | 70000 | | Sales | Henry | 80000 | | Sales | Sam | 60000 | +------------+----------+--------+
解決:three
① 使用Select Count(Distinct),內交Employee和Department兩張表,而後咱們找出比當前薪水高的最多隻能有兩個,那麼前三高的都能被取出來了。2360 msget
SELECT d.Name Department,e.Name Employee,e.Salary Salary
FROM Employee e JOIN Department d ON e.DepartmentId = d.Id
WHERE
(SELECT COUNT(DISTINCT Salary)
FROM Employee
WHERE Salary > e.Salary AND DepartmentId = d.Id) < 3
ORDER BY d.Name,e.Salary DESC;/*最多取出比當前值大的兩個數值,按照部門名遞增的順序,工資遞減的順序排序*/it
② 將上面方法中的<3換成了IN (0, 1, 2)。1608 mstable
SELECT d.Name Department,e.Name Employee,e.Salary Salary
FROM Employee e,Department d
WHERE
(SELECT COUNT(DISTINCT Salary) FROM Employee
WHERE Salary > e.Salary AND DepartmentId = d.Id) IN (0,1,2)
AND e.DepartmentId = d.Id
ORDER BY d.Name,e.Salary DESC;方法
③ 使用Group by Having Count(Distinct ..) 關鍵字。1353 ms。top
SELECT d.Name Department,e.Name Employee,e.Salary Salary
FROM
(SELECT e1.Name,e1.Salary,e1.DepartmentId
FROM Employee e1 JOIN Employee e2 ON e1.DepartmentId = e2.DepartmentId AND e1.Salary <= e2.Salary
GROUP BY e1.Id
HAVING COUNT(DISTINCT e2.Salary) <= 3) e
JOIN Department d ON e.DepartmentId = d.Id
ORDER BY d.Name,e.Salary DESC;/*使用自鏈接先取出前三個最大的工資,而後取出其所須要的內容*/
④ 給每一個人都按照薪水的高低增長一個rank,最後返回rank值小於等於3的項便可。1671 ms
SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM
(SELECT Name, Salary, DepartmentId,
@rank := IF(@pre_d = DepartmentId, @rank + (@pre_s <> Salary), 1) AS rank, @pre_d := DepartmentId, @pre_s := Salary FROM Employee, (SELECT @pre_d := -1, @pre_s := -1, @rank := 1) AS init ORDER BY DepartmentId, Salary DESC) e JOIN Department d ON e.DepartmentId = d.Id WHERE e.rank <= 3 ORDER BY d.Name, e.Salary DESC;