★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-cnhrzznx-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
SQL 架構git
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.github
+----+-------+--------+--------------+ | 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 。spa
+----+-------+--------+--------------+ | 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
表包含公司全部部門的信息。code
+----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+
編寫一個 SQL 查詢,找出每一個部門工資前三高的員工。例如,根據上述給定的表格,查詢結果應返回:htm
+------------+----------+--------+ | 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
309msblog
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
323msthree
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