LeetCode——Department Highest Salary(花式使用IN以及GROUP BY)
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)的更多相关文章
- [LeetCode] Department Highest Salary -- 数据库知识(mysql)
184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...
- [LeetCode] Department Highest Salary 系里最高薪水
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- LeetCode - Department Highest Salary
题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息 有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group ...
- 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. +----+ ...
- LeetCode DB: Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [LeetCode#184]Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- [LeetCode] Nth Highest Salary 第N高薪水
Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...
- [LeetCode] Second Highest Salary 第二高薪水
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
随机推荐
- LOBs and ORA-01555 troubleshooting (Doc ID 846079.1)
LOBs and ORA-01555 troubleshooting (Doc ID 846079.1) APPLIES TO: Oracle Database Cloud Schema Servic ...
- JDK、JVM、JRE关系
开始第一个Java程序 *保证计算机当中已经安装了文本编辑器EditPlus *安装JDK[JDK一般需要从oracle的官网下载],我们这里先用的JDK7 *在安装JDK的时候有jre JDK开发需 ...
- 动态添加Redis密码认证的方法
1.定制jedis 对redis返回的错误的处理,做两处修改: 忽略 (error) ERR Client sent AUTH, but no password is set.使配置了密码的jedis ...
- [C2] 逻辑回归(Logistic Regression)
逻辑回归(Logistic Regression) 假设函数(Hypothesis Function) \(h_\theta(x)=g(\theta^Tx)=g(z)=\frac{1}{1+e^{-z ...
- 洛谷 P4316 绿豆蛙的归宿
洛谷 P4316 绿豆蛙的归宿 洛谷传送门 题目背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 题目描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度, ...
- jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function
jQuery3.0+报错Uncaught TypeError: e.indexOf is not a function 使用.load()绑定事件时报错,Uncaught TypeError: e.i ...
- 基于docker部署flask+gunicorn+nginx
nginx安装在/etc/下,项目映射在docker中的/var/www/下 1.创建docker容器将端口映射出来,将docker外的项目映射到docker中 #docker run -it -p ...
- eclipse 离线安装SVN插件(支持eclipse201909)
1.情景展示 重装eclipse后,按照网上的在线安装方法,SVN始终安装失败,之前的离线SVN包也不能用. 2.解决方案 SVN离线包下载地址:http://subclipse.tigris.o ...
- .NET Core 中间件之压缩、缓存
前言 今天给大家介绍一下在 ASP.NET Core 日常开发中用的比较多的两个中间件,它们都是出自于微软的 ASP.NET 团队,他们分别是Microsoft.AspNetCore.Response ...
- zlib压缩相关
相关原理 deflate(RFC1951):一种压缩算法,使用LZ77和哈弗曼进行编码: zlib(RFC1950):一种格式,是对deflate进行了简单的封装,他也是一个实现库(delphi中有z ...