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. solr单机版安装与基本部署

    安装solr准备工作: linux/tomcat/jdk solr单机版的安装 1.解压缩tomcat安装包 tar -zxf tomcat-xxx.tar.gz 2.创建文件夹:mkdir /usr ...

  2. vue-cli创建的项目中引入第三方库报错 'caller', 'calle', and 'arguments' properties may not be...

    http://blog.csdn.net/sophie_u/article/details/76223978 以在vue中引入mui第三方库为例: 虽然针对vue,有单独的vue-mui库可以使用,但 ...

  3. 关于vueThink框架打包发布的一些问题

    刚开始发布自己的vueThink项目的时候,总是出现404错误,后来经过上网查找,发现是路径的问题,这方面的知识,网上很多,就不过多阐述了.我主要想说的是自己的项目发布的时候,admin账号登录的时候 ...

  4. LNMP状态管理命令

    https://lnmp.org/faq/lnmp-status-manager.html LNMP状态管理命令: LNMP 1.2+状态管理: lnmp {start|stop|reload|res ...

  5. css cursor属性-显示的光标的类型(形状)的用法和定义

    在网页布局的时候,在特定的地方,光标形状各有区别.这个时候,就需要用到css的cursor属性.根据自身需要选择设置鼠标指针样式. 定义和用法 cursor 属性规定要显示的光标的类型(形状). 该属 ...

  6. 邓_php_笔试!!!

    函数总结: ---------------------------------------------------------------------- 数组: 1.数组转换为字符串: implode ...

  7. sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别

    1.CHAR.CHAR存储定长数据很方便,CHAR字段上的索引效率级高,比如定义char(10),那么不论你存储的数据是否达到了10个字节,都要占去10个字节的空间,不足的自动用空格填充. 2.VAR ...

  8. Linux指令--rcp,scp

    rcp代表"remote file copy"(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝 ...

  9. 【转】软件开发工具介绍之 6.Web开发工具

    [本文转自http://www.cnblogs.com/dusonchen/archive/2011/02/09/1739087.html ] 1.EditPlus 无论是编写xhtml页面,还是cs ...

  10. 第三方模块paramiko的使用

    "Paramiko" is a combination of the Esperanto words for "paranoid" and "frie ...