,敢教日月换新天。为有牺牲多壮志

[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 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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
复制代码

 

posted @   为敢技术  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°