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 |
+------------+----------+--------+
# Write your MySQL query statement below
SELECT
d.`Name` AS Department,
e1. NAME AS Employee,
e1.Salary
FROM
employee e1 JOIN
department d ON e1.DepartmentId=d.Id
WHERE
3 > (
SELECT
COUNT(DISTINCT Salary)
FROM
employee e2
WHERE
e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId
)

LeetCode - 185. Department Top Three Salaries的更多相关文章

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

  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. 185. Department Top Three Salaries

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

  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. LeetCode——Department Top Three Salaries(巧妙使用子查询)

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

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

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

  7. leetcode185 Department Top Three Salaries

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

  8. [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 ...

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

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

随机推荐

  1. UE4 unreliable 同步问题

    今天发现了一个问题,标记为unreliable的函数从来不执行,但是官方文档上的说明是只有在网络负载重时才不执行此类函数,哎哎哎.

  2. SQL语句整理1

    THEN '月卡' THEN '周卡' THEN '季度卡' THEN '年卡' ELSE '未知卡类型' END AS '卡类型' FROM SL_Register_Info Left join用法 ...

  3. 学习JVM-GC收集器

    1. 前言 在上一篇文章中,介绍了JVM中垃圾回收的原理和算法.介绍了通过引用计数和对象可达性分析的算法来筛选出已经没有使用的对象,然后介绍了垃圾收集器中使用的三种收集算法:标记-清除.标记-整理.标 ...

  4. 一个域名最多能对应几个IP地址?,一个IP地址可以绑定几个域名?

    一个域名最多能对应几个IP地址?,一个IP地址可以绑定几个域名?谢谢 xikeboy | 浏览 31055 次 推荐于2016-04-24 14:21:14 最佳答案 1.也就是说通常情况下一个域名同 ...

  5. 从#65279字符看dede模板页面编码问题

    今天一位朋友让帮忙给解决一个dede模板的问题,问题主要是:模板文件生成html文件之后会在body开头处加入一个可见的控制符&#65279,导致页面头部会出现一个空白行. 接到"& ...

  6. JavaScript之BST

    自己尝试用js实现了数据结构的二叉查找树. // node function Node(data) { this.data = data; this.lc = null; this.rc = null ...

  7. PostgreSql 分页limit

    摘录自:http://jingyan.baidu.com/article/a17d528538119b8098c8f2ca.html 语法: select * from persons limit   ...

  8. Linux文件

    Linux文件类型 对于内核而言,所有打开的文件都是通过文件描述符引用(FD),文件描述符是一个非负整数,当打开现有问价或创建一个新文件时,内核向进程返回一个文件描述符. 按照惯例,shell把文件描 ...

  9. linux_链接文件

    链接概念: 分为软链接和硬链接,文件类型为 l 硬链接: ln 源文件 目标文件 软链接: ln -s 源文件 目标文件(目标文件不能事先存在) ln -s /root/ /tmp/root # 给 ...

  10. ECLIPS-S测井系统下的仪器挂接 [CV模块]

    常见简写 简写 全拼 含义 CV Calibration and Verification 刻度和校验 CP Primary Calibration 主刻度 VP Primary Verify 主校验 ...