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. 7.JavaCC官方入门指南-例2

    例2:整数加法运算--改良版(增强语法分析器) 1.修改   上一个例子中,JavaCC为BNF生产式所生成的方法,比如Start(),这些方法默认只简单的检查输入是否匹配BNF生产式指定的规范.但是 ...

  2. ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6)

    ORA-1562 and ORA-1650 Unable to Extend Rollback Segment (Doc ID 1066542.6) APPLIES TO: Oracle Databa ...

  3. nginx 安装 lua_nginx_module 模块(nginx——lua 学习笔记1)

    插入两个网站: nginx + lua 的OpenResty 开发 跟我学OpenResty(Nginx+Lua)开发目录贴 两个都是 可以根据目录一步步学习的. 1. 版本下载 nginx版本为 n ...

  4. Python xlwt模块写Excel问题集合

    1.数字转换成汉字 数据库查询返回结果为多元组,在写入Excel需要判断,数据库查询结果是否为数字,为数字的话需要将其转换成对应的汉字,此时元组不可修改,所以需要将返回结果修改成列表.实现可以在数据库 ...

  5. 03-flex-wrap是否换行

     flex-wrap:运用到父元素上 结合 display: flex; flex-wrap: wrap; 换行 flex-wrap: nowrap; 不换行 #main { width: 300px ...

  6. vue全局路由守卫beforeEach+token验证+node

    在后端安装jsonwebtoken         npm i jsonwebtoken --save 在 login.js文件中引入      // 引入jwtconst jwt = require ...

  7. 深入理解JavaScript中的作用域和上下文

    介绍 JavaScript中有一个被称为作用域(Scope)的特性.虽然对于许多新手开发者来说,作用域的概念并不是很容易理解,我会尽我所能用最简单的方式来解释作用域.理解作用域将使你的代码脱颖而出,减 ...

  8. Redis学习笔记(八、缓存设计)

    目录: 缓存更新策略 缓存粒度 缓存穿透 缓存雪崩 缓存击穿 缓存更新策略: 1.内存溢出淘汰策略 当redis的使用内存超过maxmemory时会触发相应的策略,具体策略由maxmemory-pol ...

  9. day72_10_17 序列化组件之model的运用

    一.拆分的序列化. model序列化的基本用法就是使用元类中的fields,其中model绑定的就是model中的表 如果需要多表查询,要在model中定义property: class BookMo ...

  10. C++ class内的 ++ 重载,左++,右++,重载示例。

    #include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...