[LeetCode]-DataBase-Department Top Three Salaries
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 |
+------------+----------+--------+
需求:查询每个部门,工资前三高的员工
-- 有点难,没解出来,参考LeetCode上的答案
-- 解法一、
select D.Name as Department, E.Name as Employee, E.Salary as Salary
from Employee E, Department D
where (select count(distinct(Salary)) from Employee
where DepartmentId = E.DepartmentId and Salary > E.Salary) in (0, 1, 2)
and
E.DepartmentId = D.Id
order by E.DepartmentId, E.Salary DESC;
-- 解法二、
-- 一、先查询出按 部门ID升序 工资降序 排列的数据
-- 二、把(一)查询出来的数据,再关联employee表
-- 三、再对每条工资进行统计,DISTINCT的作用是查询出并列的工资的员工
SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary
FROM Employee E, Department D
WHERE 1=1 AND (SELECT COUNT(DISTINCT(Salary)) FROM Employee
WHERE DepartmentId = E.DepartmentId AND Salary > E.Salary) < 3
AND E.DepartmentId = D.Id
ORDER BY E.DepartmentId, E.Salary DESC;
[LeetCode]-DataBase-Department Top Three Salaries的更多相关文章
- LeetCode - 185. Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- 【leetcode】Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- [LeetCode] Department Top Three Salaries 系里前三高薪水
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- LeetCode——Department Top Three Salaries(巧妙使用子查询)
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- [SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries
SQL 架构 Create table If Not Exists Employee (Id ), Salary int, DepartmentId int) Create table If Not ...
- 【SQL】185. Department Top Three Salaries
The Employee table holds all employees. Every employee has an Id, and there is also a column for the ...
- leetcode185 Department Top Three Salaries
Employee表存储员工姓名.员工所在公寓.员工工资 Department表存储公寓id 评选出各个公寓的工资前三名的员工. 遇到的问题如下: limit,in等语句不能用在嵌套select语句中, ...
- 185. Department Top Three Salaries
问题描述 解决方案 select b.name Department,a.name Employee,a.salary Salary from Employee a,Department b wher ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
随机推荐
- 洛谷 P5043 树的同构 题解
题面 本题的难度其实不及紫题的难度.主要是在hash时的处理细节比较繁琐: 首先是树hash的模板: long long treehash(int u,int fa) { ]; ; ; for(int ...
- js 获取ip和城市
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
- PostgreSQL-UNION与Order by 冲突
问题描述 union 连接的两个sql 都包含 order SELECT <property1>, <property2> FROM <table1> ORDER ...
- 一个php文件就可以把数据库的数据导出Excel表格
数据库内容太多,复制粘贴太麻烦?那就用代码实现把,把代码写好了,导出还不容易吗,访问即可导出. excel.php <?php error_reporting(E_ALL ^ E_DEPRECA ...
- golang 反射中调用方法
反射中调用函数 众所周知,golang中的函数是可以像普通的int.float等类型变量那样作为值的,例如: package main import "fmt" func hell ...
- google浏览器切换成中文
新浪下载地址:http://down.tech.sina.com.cn/content/40975.html 默认字体好像是西班牙语 1.浏览器地址chrome://settings/language ...
- openstack docker build error
1. _proto_tcp = socket.getprotobyname('tcp') OSError: protocol not found you should have a /etc/prot ...
- react中父组件给子组件传值
子组件 state = { msg: 'a' } render(){ return<h1>{this.state.msg}</h1> } setInfo = (val)=> ...
- UIWebView与JS的交互
IOS-的UIWebView UIWebVew是ios开发中较为常用的一个控件.其可以打开网页,加载html,打开文档等.当我们在开发中需要用到一些显示页面时,UIWebView是一个很好的选择. 创 ...
- initdb - 创建一个新的 PostgreSQL数据库集群
SYNOPSIS initdb [ option...] --pgdata | -D directory DESCRIPTION 描述 initdb 创建一个新的 PostgreSQL 数据库集群. ...