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的更多相关文章

  1. 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 ...

  2. 【leetcode】Department Top Three Salaries

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

  3. [LeetCode] Department Top Three Salaries 系里前三高薪水

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

  4. LeetCode——Department Top Three Salaries(巧妙使用子查询)

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

  5. [SQL]LeetCode185. 部门工资前三高的员工 | Department Top Three Salaries

    SQL 架构 Create table If Not Exists Employee (Id ), Salary int, DepartmentId int) Create table If Not ...

  6. 【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 ...

  7. leetcode185 Department Top Three Salaries

    Employee表存储员工姓名.员工所在公寓.员工工资 Department表存储公寓id 评选出各个公寓的工资前三名的员工. 遇到的问题如下: limit,in等语句不能用在嵌套select语句中, ...

  8. 185. Department Top Three Salaries

    问题描述 解决方案 select b.name Department,a.name Employee,a.salary Salary from Employee a,Department b wher ...

  9. 【LeetCode】692. Top K Frequent Words 解题报告(Python)

    [LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...

随机推荐

  1. 记一次程序从x86_64linux平台移植到armv7平台

    前言 最近接了个任务,需要把代码移植到armv7平台,搜寻相关方法,了解到可以利用交叉编译工具如:gcc-linaro-arm-linux-gnueabihf.把自己依赖的第三方库代码和自己代码分别编 ...

  2. div距离左边设置

    margin-right:不加负号, margin-left:必须加负号,理解为倒数 margin-left:-10px;

  3. 2015 四川省赛 C Censor(哈希 | KMP)

    模式串为子串 KMP /* @author : victor */ #include <bits/stdc++.h> using namespace std; typedef long l ...

  4. oracle常用函数(1)

    oracle有很强大的函数功能,常用的字符处理函数如下: SQL> select initcap('hello') from dual;//将首字母转为大写 INITCAP('HELLO')-- ...

  5. Wine-Staging 4.9 发布,增添一些新补丁

    Wine-Staging的工作还在继续,到4.9版为止,在上游Wine代码库上有超过830个补丁. 在上周五的Wine 4.9发布之后,Wine-Staging 4.9已经发布了,之前的一些暂存工作现 ...

  6. puppet之资源

    资源 资源的定义 一个帐号,一个文件,目录,软件包都可以称作是资源,每个资源的定义都具有标题,类型,以及一些列属性. 常见的资源有notify(调试与输出),file(配置文件),package(软件 ...

  7. Linux RAID磁盘阵列

    RAID磁盘阵列 什么是RAID RAID是磁盘阵列的英文缩写,多块磁盘组成了一个组合,一起完成存储任务,就是磁盘阵列. RAID几种常用的类别(组合) RAID0:条带卷:最低磁盘个数2+,空间利用 ...

  8. SpringCloud系列(一):Eureka 注册中心

    在演示spring cloud之前得要知道我们为什么需要微服务框架. 先讲讲我的经历,以前我们做项目时所有功能都写在一起,只是做了分层(模型,数据,业务),所有业务逻辑都写在业务层,刚开始还好,等时间 ...

  9. AI换脸教程:DeepFaceLab使用教程(2.训练及合成)

    如果前期工作已经准备完毕(DeepFaceLab下载(https://www.deepfacelabs.com/list-5-1.html),然后安装相应的显卡驱动,DeepFaceLab使用教程(1 ...

  10. SetConsoleTextAttribute和SetConsoleScreenBufferInfoEx的使用

    主要是作用于控制台文本下划线和改变文本颜色 #include "pch.h" #include <iostream> #include <Windows.h> ...