[LeetCode] Department Highest Salary -- 数据库知识(mysql)
184. Department Highest Salary
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 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | 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, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
解答一:
SELECT D.Name AS Department ,E.Name AS Employee ,E.Salary
from
Employee E,
Department D
WHERE E.DepartmentId = D.id
AND (E.DepartmentId,E.Salary) in
(SELECT DepartmentId,max(Salary) as max FROM Employee GROUP BY DepartmentId)
解题思路是利用WHERE连接子句来构造筛选条件,子句汇中构造了一个新的表max,用到了IN操作符来选择DepartmentId 和Salary,group by 用来根据DepartmentId分组以便分别
求每个系的最高薪水。
解答二:
SELECT D.Name AS Department ,E.Name AS Employee ,E.Salary
FROM
Employee E,
(SELECT DepartmentId,max(Salary) as max FROM Employee GROUP BY DepartmentId) T,
Department D
WHERE E.DepartmentId = T.DepartmentId
AND E.Salary = T.max
AND E.DepartmentId = D.id
和一稍有不同,直接从三个表中选取,将新构造的表放到了FROM子句里边,其他直接以别名取代,以and连接三个条件。
[LeetCode] Department Highest Salary -- 数据库知识(mysql)的更多相关文章
- [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(花式使用IN以及GROUP BY)
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. +----+ ...
- Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数
mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...
- 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 ...
随机推荐
- spring4笔记----spring4设值注入
2个接口 package com.ij34.servce; public interface people { public void cut(); } package com.ij34.servce ...
- [20181108]with temp as 建立临时表吗.txt
[20181108]with temp as 建立临时表吗.txt --//链接:http://www.itpub.net/thread-2106304-1-1.html--//作者提到在dg上使用w ...
- C#-判断语句(五)
判断语句主要有if...else.switch和 条件?语句1:语句2 三种,而if...else中又有if语句,if...else.if...else if...else和if中嵌套if这几种,但是 ...
- ASP.NET中的参数与特殊类型和特性
一.可选参数和命名参数 1.可选参数 语法: [修饰符] 返回类型 方法名(必选参数1...必选参数n,可选参数1...可选参数n) ...
- 高性能JavaScript模板引擎 artTemplate
下载地址 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <ti ...
- Amazon onsite behavior question
https://www.1point3acres.com/bbs/thread-307462-1-1.html http://kraftshala.com/how-to-raise-the-bar-i ...
- linux学习笔记整理(二)
第三章 Linux基本命令操作本节所讲内容:3.1 Linux终端介绍 Shell提示符 Bash Shell基本语法3.2 基本命令的使用:ls.pwd.cd.history3.3 查看系统和BIO ...
- vue数据绑定数组,改变元素时不更新view问题
关于这个问题,官网上说的很清楚官方文档 写个例子HTML<body> <div class="box"> <div v-for="aa i ...
- python实现命令行解析的argparse的使用
参考https://docs.python.org/3.6/library/argparse.html argparse模块使编写用户友好的命令行界面变得很容易.程序定义了它需要什么参数,argpar ...
- 利用BBED恢复数据文件头
转载请注明出处:http: @@@@@@@利用BBED模拟损坏5文件1号块(文件头) BBED block block ) Block: Dba:0x01400001 ---------------- ...