[SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10172119.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

SQL 架构

 1 Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int)
 2 Create table If Not Exists Department (Id int, Name varchar(255))
 3 Truncate table Employee
 4 insert into Employee (Id, Name, Salary, DepartmentId) values ('1', 'Joe', '70000', '1')
 5 insert into Employee (Id, Name, Salary, DepartmentId) values ('2', 'Henry', '80000', '2')
 6 insert into Employee (Id, Name, Salary, DepartmentId) values ('3', 'Sam', '60000', '2')
 7 insert into Employee (Id, Name, Salary, DepartmentId) values ('4', 'Max', '90000', '1')
 8 Truncate table Department
 9 insert into Department (Id, Name) values ('1', 'IT')
10 insert into Department (Id, Name) values ('2', 'Sales')

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+
| 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.

+----+----------+
| 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  |
+------------+----------+--------+

Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id 。

+----+-------+--------+--------------+
| 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            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门工资前三高的员工。例如,根据上述给定的表格,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

302ms
 1 # Write your MySQL query statement below
 2 
 3 SELECT D.Name AS Department,E.Name as Employee,Salary
 4 FROM (
 5 SELECT IF(@pre=DepartmentId,IF(@pre_salary<=>@pre_salary:=Salary,@rank,@rank:=@rank+1),@rank:=1) AS Rank,
 6     Name,Salary,@pre:=DepartmentId AS DepartmentId
 7 FROM Employee,(SELECT @pre:=0,@rank:=0,@pre_salary:=NULL) v
 8 ORDER BY DepartmentId,Salary DESC
 9 ) E
10 INNER JOIN Department D
11 ON E.DepartmentId=D.Id
12 WHERE Rank<=3

309ms

1 select d.Name as Department, e.Name as Employee, Salary
2 from Employee e join Department d on e.DepartmentId = d.Id
3 where 3 >= (
4     select count(*)
5     from (select distinct DepartmentId as dept_id, Salary from Employee) as e1
6     where e1.dept_id = e.DepartmentId and e1.salary >= e.salary
7 )
8 order by Department, e.Salary desc

323ms

 1 # Write your MySQL query statement below
 2 
 3 # 99.7%
 4 select d.Name Department, e.Name Employee, e.Salary
 5 from (  select @rn := case when @dept = DepartmentId and @sal =  Salary then @rn 
 6                            when @dept = DepartmentId and @sal <> Salary then @rn + 1
 7                            else 1 end  rows,
 8                @dept := DepartmentId, @sal := Salary, employee.*
 9         from employee, (select @rn := 0, @dept := 0, @sal:= -1) t
10         order by DepartmentId, Salary desc) e,
11       Department d
12 where d.Id = e.DepartmentId
13 and e.rows <= 3
14 order by Department, Salary desc;

325ms

 1 # Write your MySQL query statement below
 2 select Name as Department, Employee, Salary
 3 from (
 4         select Name as Employee, Salary, DepartmentId as Id,
 5             @rank := if(@id = (@id := DepartmentId), @rank, 0)
 6                 + (@salary <> (@salary := Salary)) as Rank
 7         from Employee, (select @rank := 0, @id := -1, @salary := -1) init
 8         order by DepartmentId, Salary desc
 9     ) as rankek join Department using (Id)
10 where Rank <= 3
原文地址:https://www.cnblogs.com/strengthen/p/10172119.html