博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
185. Department Top Three Salaries (Hard)
阅读量:4697 次
发布时间:2019-06-09

本文共 1417 字,大约阅读时间需要 4 分钟。

Source: 
Description:

The Employee table holds all employees. Every employee has an Id, and there is also a column for the department Id.

+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId |+----+-------+--------+--------------+| 1 | Joe | 70000 | 1 || 2 | Henry | 80000 | 2 || 3 | Sam | 60000 | 2 || 4 | Max | 90000 | 1 || 5 | Janet | 69000 | 1 || 6 | Randy | 85000 | 1 |+----+-------+--------+--------------+

The Department table holds all departments of the company.

+----+----------+| Id | Name |+----+----------+| 1 | IT || 2 | Sales |+----+----------+

Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.

+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT | Max | 90000 || IT | Randy | 85000 || IT | Joe | 70000 || Sales | Henry | 80000 || Sales | Sam | 60000 |+------------+----------+--------+

 

Solution:
select t2.name  as Department, t1.Employee , t1.salary from (    select e1.id, e1.name as employee, e1.salary, e1.departmentid from Employee  e1    inner join  Employee e2    on e1.departmentid = e2.departmentid    and e1.salary<=e2.salary    group by e1.id    having count(distinct(e2.salary))<=3)t1inner join(select * from Department )t2on t1.DepartmentId  = t2.id

转载于:https://www.cnblogs.com/sixu/p/6884906.html

你可能感兴趣的文章
第三周总结
查看>>
流程控制与数组
查看>>
python循环导入的解决方案
查看>>
AngularJS中选择样式
查看>>
JDK的命令具体解释操作
查看>>
创建图书管理项目
查看>>
ie6 双边距问题
查看>>
Vue跨层级传递slot的方法
查看>>
暑假集训计划
查看>>
做个睿智的女人
查看>>
List遍历三种方法:1.for 2.增强性for 3.迭代器
查看>>
ArcGIS10.1发布WFS-T服务
查看>>
leetcode-汉明距离
查看>>
在Mac机器上给ITerm2配置lrzsz,便捷的传输文件到远程服务器上
查看>>
软工文档——系统、程序流程图、系统流图
查看>>
Hashtable的hashCode的使用以及toString的复写——《Thinking in Java》随笔024
查看>>
list remove object
查看>>
POJ3683 Priest John's Busiest Day
查看>>
linux基本命令(2) 修改文件所属人以及权限
查看>>
.net 数据依赖缓存
查看>>