leetcode-185. 部门工资前三高的所有员工

该篇文章介绍了如何使用SQL查询LeetCode竞赛中的一个问题,即找出每个部门中工资排名前三的员工。通过子查询和计数唯一员工来确定工资排名,最后返回包含部门名、员工名和工资的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

https://leetcode.cn/problems/department-top-three-salaries/description/

# 部门工资前三高的所有员工
# 1.最外层的表
#select as Department, as Employee, as Salary from xxx where ;
# 2.工资比谁高, 需要有个比较的对象,相同的表用2次进行比较.
## 工资前三高, 意味着 比 e1.salary 高的数量只能是 0, 1, 2个
# (select count(distinct e2.name) from Employee e2 where e2.salary > e1.salary) < 3
# 3.每个部门, 意味着工资比较是在同一部门内, 此时 e1和e2保证部门id 一样
# (select count(distinct e2.name) from Employee e2 where e2.salary > e1.salary and e1.departmentId = e2.departmentId) < 3
# 注意 count(distinct e2.name) 改为 count(distinct e2.salary) 是钱的前三,同样的钱算一个名次
# 4.输出结果
select d.name as Department,e1.name as Employee, e1.salary as Salary from Employee e1 left join Department d on e1.departmentId = d.id
where 
(select count(distinct e2.salary) from Employee e2 where e2.salary > e1.salary and e1.departmentId = e2.departmentId) < 3 ;
```

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值