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. scrapy学习笔记(一)

    环境:Windows 7  x64   Python3.7.1  pycharm 一.安装scrapy 1.1linux系统使用:pip install scrapy 1.2Windows系统: pi ...

  2. 使用qwt作曲线图——有网格线背景的画法

    创建自己的QwtPlot (1)     增加一个类比如curvePlotWidget,继承自QwtPlot (2)代码示例 curvePlotWidget::curvePlotWidget(QWid ...

  3. shell脚本实现自动化安装linux版本的loadrunner agent(centos6.8)

    #!/bin/bash #Centos6下安装LoadRunner负载机 #@author Agoly #@date #@source 高级测试技术交流圈: yum -y install expect ...

  4. 代码方式给控件添加背景图片(WPF)

    wpf中常常需要给控件添加背景图片,下边以wrapPanel为例,使用代码添加背景图片 WrapPanel xwraPanel = new WrapPanel(); ImageBrush ximgBr ...

  5. bootstrap的下拉菜单组件与导航条

    前期准备:bootstrap的css文件和js文件先引入 Bootstrap 组件-拉下菜单(class+js) 下拉菜单必需三级结构 <div class="dropdown&quo ...

  6. linux中利用speedtest-cli测网速

    [root@elegant-snap-3 you-get]# wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/spe ...

  7. springcloud学习之路: (四) springcloud集成Hystrix服务保护

    Hystrix是一套完善的服务保护组件, 可以实现服务降级, 服务熔断, 服务隔离等保护措施 使用它可以合理的应对高并发的情况 做到保护服务的效果 1. 导入依赖 <dependency> ...

  8. Java八大排序之希尔(Shell)排序

    希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该 ...

  9. zz2019年主动学习有哪些进展?答案在这三篇论文里

    2019年主动学习有哪些进展?答案在这三篇论文里 目前推广应用的机器学习方法或模型主要解决分类问题,即给定一组数据(文本.图像.视频等),判断数据类别或将同类数据归类等,训练过程依赖于已标注类别的训练 ...

  10. 【oracle】11g服务器安装详细步骤

    以下是百度经验:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 1.同时解压 2.setup 3.