Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
+----+-------+--------+--------------+

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

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

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

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

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

我的解答:

# Write your MySQL query statement below

select
d.Name as Department,
temp.Name Employee,
temp.Salary
from
Department d left join
(select
e.DepartmentId,
e.Name,
@curRank := if (@prevDept = DepartmentId, if(@prevSal = e.Salary, @curRank, @curRank + 1), 1) as Rank,
@prevSal := e.Salary as Salary,
@prevDept := e.DepartmentId
from Employee e,
(select @prevDept := null, @curRank := 0, @prevSal := null) t
order by e.DepartmentId, e.Salary desc
) temp on d.Id = temp.DepartmentId
where temp.Rank <= 3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/department-top-three-salaries

: )

部门工资前三高的所有员工 - LeetCode的更多相关文章

  1. 185. 部门工资前三高的所有员工 + 多表联合 + join + dense_rank()

    185. 部门工资前三高的所有员工 LeetCode_MySql_185 题目描述 方法一:使用join on # Write your MySQL query statement below sel ...

  2. Leetcode的SQL题解:185. 部门工资前三高的员工

    题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...

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

    SQL 架构 Create table If Not Exists Employee (Id ), Salary int, DepartmentId int) Create table If Not ...

  4. sql查询:部门工资前三高的员工和部门工资最高的员工

    创建表:Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);Cr ...

  5. SQL查询每个部门工资前三名的员工信息

    --通用sql select deptno, ename, sal from emp e1 where ( ) from emp e2 where e2.deptno=e1.deptno and e2 ...

  6. mysql查询每个部门/班级前几名

    Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id . +----+-------+--------+--------------+ | I ...

  7. LeetCode:184.部门工资最高的员工

    题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...

  8. leetcode 184 部门工资最高的员工

    题目描述:Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. Department 表包含公司所有部门的信息. 编写一个 SQL 查询,找 ...

  9. [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

随机推荐

  1. m3u8的blob格式视频在线下载

    有时候我们希望在在线观看视频的同时将对应的视频下载下来,研究了很多方式,最终发现使用ffmpeg这个工具可以很好完成m3u8格式. 具体方法就是执行: ffmpeg -i https://cdn-ho ...

  2. VS2010 报错该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失

    尤其代码是从linux平台复制过来: 报错如图: 更有甚者基本函数都报错: 当下检查发现if else break case等基本函数并无问题时,报错行数明显不一致等一定要注意文档编码格式, 最简单的 ...

  3. Linux signal与定时器

    1. signal sighandler_t signal(int signum, sighandler_t handler); signum:是一个信号.除了SIGKILL和SIGSTOP外的任何一 ...

  4. Java垃圾回收。

    一:如何确定哪些对象应该被回收.  1.引用记数法.在对象中添加一个引用计数器,每当有一个地方引用它时,计数器加一,引用失效时,计数器减一,当计数器为0时,该对象是不可用的.   i:缺陷:会产生循环 ...

  5. python简单日志处理

    简单日志处理 import datetime import re logfile='''58.61.164.141 - - [22/Feb/2010:09:51:46 +0800] "GET ...

  6. 使用Visual Studio学习C语言

    注明:安装的是社区版,只写大部分步骤,做笔记之用.详细还需要看B站教程,https://www.bilibili.com/video/av59608520 一.安装软件 1.安装Visual Stud ...

  7. 设备树的规范(dts)

    设备树的官方文档: https://www.devicetree.org/specifications/ 一.理论部分 如何看下面这张图: 1)从根节点开始看起,即 / 2)在根节点中有属性以及设备节 ...

  8. Appium+python自动化(二)- 环境搭建—下(超详解)

    简介 宏哥的人品还算说得过去,虽然很久没有搭建环境了,但是换了新电脑设备,一气呵成,将android的测试开发环境已经搭建准备完毕.上一篇android测试开发环境已经准备好, 那么接下来就是appi ...

  9. VLDB 2019:

    纵览数据库顶会VLDB 2019论文,我们发现了六大发展动向 作者 | 韩硕 [导读]一年一度的数据库领域顶级会议 VLDB 2019 于当地时间8月26日-8月30日在美国加利福尼亚州洛杉矶召开,探 ...

  10. VIJOS-P1078 松鼠吃果子

    洛谷 P2069 松鼠吃果子 https://www.luogu.org/problemnew/show/P2069 JDOJ 1256: VIJOS-P1078 松鼠吃果子 https://neoo ...