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. opencv学习之等待按键事件-waitKey函数

    文章来源: https://mangoroom.cn/opencv/opencv-learning-waitKey.html 序 waitKey函数属于opencv函数里既常用又非常基础的函数,无论是 ...

  2. python之代码规范

    第一章 为什么要有规范化目录 真正的后端开发的项目,系统等,少则几万行代码,多则十几万,几十万行代码 软件开发,规范你的项目目录结构,代码规范,遵循PEP8规范等等,让你更加清晰,合理开发. 1.代码 ...

  3. 如何利用`keep-alive`按需缓存页面数据

    随着项目不断变大,页面变多,搜索条件也随之也越来越多,而每次跳转页面再返回时,之前的筛选的条件都会别清空.之前在elment-ui table组件 -- 远程筛选排序提到过缓存,但是有所取巧,这次重新 ...

  4. npm学习(二)之如何防止权限错误

    如何防止权限错误 如果您在尝试全局安装包时看到EACCES错误,请阅读本章.如果更改安装npm的目录,通常可以避免此错误.要做到这一点,要么使用版本管理器重新安装npm(推荐)或手动更改npm的默认目 ...

  5. LOCATE()和FIND_IN_SET()

    locate(substr,str) 查找substr在str中第一次出现的位置,为空时返回0 SELECT login_name, LOCATE('ning',login_name) FROM us ...

  6. java程序员面试宝典1

    1.在java中字符串只以Unicode一种形式存在(不选择任何特定的编码,直接使用他们在字符集中的编号,这是统一的唯一的方法) 2.在java中,是指在JVM中,在内存中,在你的代码里声明的每个ch ...

  7. es6 filter() 数组过滤方法总结(转载)

    1.创建一个数组,判断数组中是否存在某个值 var newarr = [ { num: 1, val: 'ceshi', flag: 'aa' }, { num: 2, val: 'ceshi2', ...

  8. 利用wampserve搭建本服务器

    1.官网下载安装包 注意:3.0.6版本需要下载依赖包vc依赖包 2.默认为英文 右击图标进入langue设置为中文 3.需要手动设置在现状态 右击=>选中wampsetting =>me ...

  9. 几个常用I/O函数用法(printf,fprintf等)

    一 格式化输出 1.printf 定义:int printf(const char *format,[argument]); 功能:产生格式化输出的函数(定义在 stdio.h 中) 参数说明:for ...

  10. Python爬虫之定时抢购淘宝商品

    Python爬虫之定时抢购淘宝商品 import time from selenium import webdriver import datetime class Spider: def __ini ...