跳到主要内容

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

链接:185.部门工资前三高的所有员工
难度:Hard
标签:数据库
简介:编写解决方案,找出每个部门中 收入高的员工 。

题解 1 - sql

  • 编辑时间:2024-10-16
  • 执行用时:1431ms
  • 编程语言:sql
  • 解法介绍:利用子查询查找比当前薪水大的人数,如果小于3那就说明当前人是前三
select
d1.name as Department,
e1.name as Employee,
e1.salary as Salary
from Employee e1 left join Department d1 on e1.departmentId = d1.id
where 3 > (
select count(distinct e2.salary)
from
Employee e2 left join Department d1 on e1.departmentId = d1.id
where
e1.departmentId = e2.departmentId and
e2.salary > e1.salary
)