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

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

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  |+------------+----------+--------+
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', '70000', '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')
Truncate table Department
insert 
into Department (Id, Name) values ('1', 'IT')
insert into Department (Id, Name) values ('2', 'Sales')
 

 
select d.Name Department, e1.Name Employee, e1.Salaryfrom Employee e1 join Department don e1.DepartmentId = d.Id where 3 > (select count(distinct(e2.Salary)) from Employee e2 where e2.Salary > e1.Salary and e1.DepartmentId = e2.DepartmentId );

转载于:https://www.cnblogs.com/vicky-project/p/9178787.html

你可能感兴趣的文章
[总结]2019年10月 OI学习/刷题记录
查看>>
[BZOJ4870/LOJ2143][Shoi2017]组合数问题
查看>>
切词框架jcseg,入门
查看>>
深入理解Spring Redis的使用 (一)、Spring Redis基本使用
查看>>
windows如何安装mysql
查看>>
Class.getResourceAsStream()与ClassLoader.getResourceAsStream()获取资源时的路径说明
查看>>
玩转深拷贝/浅拷贝
查看>>
java之反射
查看>>
博客阅读笔记
查看>>
[恢]hdu 1050
查看>>
订阅 memcached: error while loading shared libraries: libevent-2.0.so.5: cannot o解决
查看>>
《入门经典》——8.4
查看>>
《University Calculus》-chape4-导数的应用-微分中值定理
查看>>
创业者的思维误区---《精益创业》
查看>>
Web app 的性能瓶颈与性能调优方法
查看>>
经纬度计算是否在圆形内,是否在矩形内,是否在多边形内方法
查看>>
python3.x 和 python2.x关于 urllib的用法
查看>>
网站搭建(二)
查看>>
VS2010插件
查看>>
[转]win7下修改C盘USERS文件下的名称
查看>>