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

+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2  | Jim   | 90000  | 1            |
| 3 | Henry | 80000 | 2 |
| 4 | Sam | 60000 | 2 |
| 5 | Max | 90000 | 1 |
+----+-------+--------+--------------+
The Department table holds all departments of the company. +----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, your SQL query should return the following rows (order of rows does not matter). +------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT         | Jim      | 90000  |
| Sales | Henry | 80000 |
+------------+----------+--------+

以前使用IN,都是局限于单个数值使用,从未尝试过多个数据使用IN.

此题涉及两个表,肯定需要使用join操作.

此外,需要选取每个Department 的最大数值,那么肯定涉及max以及group by操作.

综合以上因素,答案如下所示:

# Write your MySQL query statement below
SELECT Employee.Name AS Employee, Employee.Salary, Department.Name AS Department
FROM Employee, Department
WHERE
Employee.DepartmentId = Department.Id
AND (Employee.DepartmentId, Employee.Salary)
IN
(SELECT Employee.DepartmentId, max(Employee.Salary)
FROM Employee
GROUP BY Employee.DepartmentId);

LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)的更多相关文章

  1. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  2. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  3. LeetCode - Department Highest Salary

    题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息 有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group ...

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. LeetCode DB: Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  6. [LeetCode#184]Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  7. [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  8. [LeetCode] Nth Highest Salary 第N高薪水

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  9. [LeetCode] Second Highest Salary 第二高薪水

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

随机推荐

  1. [20191012]组成rowid.txt

    [20191012]组成rowid.txt --//昨天做了拆分rowid的测试,链接http://blog.itpub.net/267265/viewspace-2659613/=>[2019 ...

  2. Shell命令-搜索文件或目录之whereis、locate

    文件及内容处理 - whereis.locate 1. whereis:查找二进制命令,按环境变量PATH路径查找 whereis命令的功能说明 whereis 命令用于查找文件.该指令会在特定目录中 ...

  3. jQuery中的CSS(四)

    1. css(name|pro|[,val|fn]), 访问匹配元素的样式属性 jQuery 1.8中,当你使用CSS属性在css()或animate()中,我们将根据浏览器自动加上前缀(在适当的时候 ...

  4. C语言——初见指针

    指针 指针是存放变量地址的变量. 由此我们可以知道了,指针存放的是变量地址且只能应用于内存中对象,指针本身也是一个变量.但是指针不仅仅可以存储变量地址,还可以是其他例如函数指针. C语言程序运行时,内 ...

  5. 剑指offer:二叉搜索树的第k个结点(中序遍历)

    1. 题目描述 /* 给定一棵二叉搜索树,请找出其中的第k小的结点. 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. */ 2. 思路 中序遍历二叉搜索树,第K个就 ...

  6. 『卧槽』意外发现了 Hashtable 的 foreach 用法 BUG

    这段时间,公司项目中 遇到一个问题,最后查出: 是 Hashtable 的用法导致的. private static void AutoCleanCache() { try { lock (m_Has ...

  7. Spark学习笔记01-基础

    目录 简介 特性 Spark运行模式 Mac本地安装 本文基于 Spark 2.4.1 进行演示,相关代码可以在我的Github上看到. 简介 Spark是一个分布式集群计算系统,类似Hadoop提供 ...

  8. POJ 3252 (数位DP)

    ###POJ 3252 题目链接 ### 题目大意:给你一段区间 [Start,Finish] ,在这段区间中有多少个数的二进制表示下,0 的个数 大于等于 1 的个数. 分析: 1.很显然是数位DP ...

  9. Composer 的安装

    最近在家休息了两个月,本来打算看看书,结果和朋友做了个小项目.项目也差不多接近尾声了,就准备找工作了,朋友推荐我去他们公司做事,不过是使用 PHP 进行开发了.我这一年来使用 Java 进行开发,今后 ...

  10. RoadMap:如何创建产品路线图

    (1)什么是Roadmap? RoadMap/产品路线图 Roadmap通常翻译为“路线图”或“蓝图”,目前并没有一个公认的定义.在这里,我们认为Roadmap是产品经理进行产品管理的一个中长期规划, ...