当前位置:网站首页>185. 部门工资前三高的所有员工(必会)
185. 部门工资前三高的所有员工(必会)
2022-07-27 00:43:00 【only-qi】
问题描述:
表: Employee
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
Id是该表的主键列。
departmentId是Department表中ID的外键。
该表的每一行都表示员工的ID、姓名和工资。它还包含了他们部门的ID。
表: Department
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
Id是该表的主键列。
该表的每一行表示部门ID和部门名。
公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。
编写一个SQL查询,找出每个部门中 收入高的员工 。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例:
示例 1:
输入:
Employee 表:
+----+-------+--------+--------------+
| 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 |
+----+-------+
输出:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Joe | 85000 |
| IT | Randy | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
解释:
在IT部门:
- Max的工资最高
- 兰迪和乔都赚取第二高的独特的薪水
- 威尔的薪水是第三高的
在销售部:
- 亨利的工资最高
- 山姆的薪水第二高
- 没有第三高的工资,因为只有两名员工
通过次数96,292
提交次数186,076
上SQL,拿去即可运行:
CREATE TABLE `Employee` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` decimal(10,2) DEFAULT NULL,
`departmentId` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
CREATE TABLE `department` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;select
d.Name as Department, e1.Name as Employee, e1.Salary
from
Employee e1 join department d on e1.departmentId = d.Id
where
3 > (select
COUNT(distinct e2.Salary)
from Employee e2
where e2.Salary > e1.Salary
and e1.DepartmentId = e2.DepartmentId );运行结果:

我要刷300道算法题,第109道 。
其实什么都没有,就只是懒--共勉!
边栏推荐
- CS224W fall 1.2 Applications of Graph ML
- Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
- Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
- 数据资产管理的概念
- 次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
- 百度云人脸识别
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- 基于GoLang实现API短信网关
- The EXE compiled by QT is started with administrator privileges
- Rust Web(一)—— 自建TCP Server
猜你喜欢

Cloud development sleeping alarm clock wechat applet source code

对象创建的流程分析
![[paper]PointLaneNet论文浅析](/img/f6/8001be4f90fe15100e0295de02491f.png)
[paper]PointLaneNet论文浅析

Lua函数之非全局函数

vs2019 中编译和使用 protobuf 库

The EXE compiled by QT is started with administrator privileges

使用 WebSocket 实现一个网页版的聊天室(摸鱼更隐蔽)

Play a parallel multithreaded mcu-mc3172

八皇后编程实现

Alibaba cloud technology expert Yang Zeqiang: Construction of observability on elastic computing cloud
随机推荐
[paper]PointLaneNet论文浅析
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
对象创建的流程分析
Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name
[二分查找中等题] LeetCode 34. 在排序数组中查找元素的第一个和最后一个位置
Kubernetes Dashboard 部署应用以及访问
基于GoLang实现API短信网关
Go to export excel form
Ansible series: do not collect host information gather_ facts: False
身家破亿!86版「红孩儿」拒绝出道成学霸,已是中科院博士,名下52家公司
Debezium series: the binlog file cannot be recovered after the record is hung from the library server, and the task is switched to the main library to ensure that the data is not lost
Swiperjs custom width
Coco test dev test code
cocos小游戏实战-05-NPC与角色攻击逻辑
MySQL master-slave database configuration based on docker for Ubuntu
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
数据资产管理的概念
[SQL简单题] LeetCode 627. 变更性别
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议