当前位置:网站首页>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道 。
其实什么都没有,就只是懒--共勉!
边栏推荐
- Debezium系列之:记录从库服务器挂掉后binlog文件无法恢复,任务切换到主库并保证数据不丢失的方法
- Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
- B-树的应用以及添加和删除操作
- Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- I heard that you knelt on the interface test during the interview?
- static关键字
- Database knowledge required by testers: MySQL common syntax
- Cloud development sleeping alarm clock wechat applet source code
- Thread.Sleep(0)的作用
猜你喜欢

CS224W fall 1.2 Applications of Graph ML

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

MySQL master-slave database configuration based on docker for Ubuntu

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Play a parallel multithreaded mcu-mc3172

vs2019 中编译和使用 protobuf 库

Blog competition dare to try BAC for beginners

哈希表与一致性哈希的原理理解以及应用
![[paper]PointLaneNet论文浅析](/img/f6/8001be4f90fe15100e0295de02491f.png)
[paper]PointLaneNet论文浅析

How to use devaxpress WPF to create the first MVVM application in winui?
随机推荐
批量复制宝贝上传提示乱码,如何解决?
Kubernetes dashboard deployment application and access
Baidu cloud face recognition
shell(38) : ssh端口转发
Time module: acquisition and mutual transformation of timestamp, structured time and formatted time
Pyqt5 use pyqtgraph to draw dynamic scatter chart
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 53. 最大子数组和
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
【RYU】安装RYU常见问题及解决办法
CS224W fall 1.2 Applications of Graph ML
如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?
关于url编解码应该选用的函数
Knowledge points of test questions related to software testing
Information collection port scanning tool nmap instructions
论构造函数的原型是谁
[binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
万字长文,带你搞懂 Kubernetes 网络模型
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
数据库读写分离和分库分表