sql 部门工资前三高的所有员工

该SQL查询用于确定每个部门中收入排名前三的员工。首先,使用窗口函数dense_rank()按部门分组并根据薪水降序排列员工。然后,筛选出排名在前三的员工,最后通过JOIN操作连接Department和Employee表,显示部门名称、员工姓名及其薪水。
摘要由CSDN通过智能技术生成

 

Create table If Not Exists Employee (id int, name varchar(255), salary int, departmentId int)

Create table If Not Exists Department (id int, name varchar(255))

Truncate table Employee

insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '85000', '1')

insert into Employee (id, name, salary, departmentId) values ('2', 'Henry', '80000', '2')

insert into Employee (id, name, salary, departmentId) values ('3', 'Sam', '60000', '2')

insert into Employee (id, name, salary, departmentId) values ('4', 'Max', '90000', '1')

insert into Employee (id, name, salary, departmentId) values ('5', 'Janet', '69000', '1')

insert into Employee (id, name, salary, departmentId) values ('6', 'Randy', '85000', '1')

insert into Employee (id, name, salary, departmentId) values ('7', 'Will', '70000', '1')

Truncate table Department

insert into Department (id, name) values ('1', 'IT')

insert into Department (id, name) values ('2', 'Sales')

公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。

编写一个SQL查询,找出每个部门中 收入高的员工 。

以 任意顺序 返回结果表。

查询结果格式如下所示

 

select d.name Department,e.name Employee,e.Salary from Department d join Employee e on e.departmentid=d.id and (e.departmentid,e.name) in 

(select departmentid,name from 

(select departmentid,name,dense_rank() over(partition by departmentid order by salary desc) as rk from Employee) as t1 where rk<4);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值