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 |
+------------+----------+--------+

这道题让给了我们两张表,Employee表和Department表,让我们找系里面薪水最高的人的,实际上这题是Second Highest SalaryCombine Two Tables的结合题,我们既需要联合两表,又要找到最高薪水,那么我们首先让两个表内交起来,然后将结果表需要的列都标明,然后就是要找最高的薪水,我们用Max关键字来实现,参见代码如下:

解法一:

SELECT d.Name AS Department, e1.Name AS Employee, e1.Salary FROM Employee e1
JOIN Department d ON e1.DepartmentId = d.Id WHERE Salary IN
(SELECT MAX(Salary) FROM Employee e2 WHERE e1.DepartmentId = e2.DepartmentId);

我们也可以不用Join关键字,直接用Where将两表连起来,然后找最高薪水的方法和上面相同:

解法二:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE e.DepartmentId = d.Id AND e.Salary = (SELECT MAX(Salary) FROM Employee e2 WHERE e2.DepartmentId = d.Id);

下面这种方法没用用到Max关键字,而是用了>=符号,实现的效果跟Max关键字相同,参见代码如下:

解法三:

SELECT d.Name AS Department, e.Name AS Employee, e.Salary FROM Employee e, Department d
WHERE e.DepartmentId = d.Id AND e.Salary >= ALL (SELECT Salary FROM Employee e2 WHERE e2.DepartmentId = d.Id);

类似题目:

Second Highest Salary

Combine Two Tables

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Department Highest Salary 系里最高薪水的更多相关文章

  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] Second Highest Salary 第二高薪水

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

  3. 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 ...

  4. LeetCode - Department Highest Salary

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

  5. 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. +----+ ...

  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. [LeetCode] Nth Highest Salary 第N高薪水

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

  8. LeetCode DB: Department Highest Salary

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

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

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

随机推荐

  1. 一个前端所需具备的PS能力

    前端网页设计+静态实现案例 放一个2天半内给某公司完成的(设计 + 静态实现)的案例吧,静态阴影用CSS3实现的http://www.cnblogs.com/MuYunyun/p/5693615.ht ...

  2. (转)从P1到P7——我在淘宝这7年

    (一) 2011-12-08 [原文链接] 今天有同事恭喜我,我才知道自己在淘宝已经七周年了.很多人第一句话就是七年痒不痒,老实说,也曾经痒过,但往往都是一痒而过,又投入到水深火热的工作中去.回家之后 ...

  3. DataAccess通用数据库访问类,简单易用,功能强悍

    以下是我编写的DataAccess通用数据库访问类,简单易用,支持:内联式创建多个参数.支持多事务提交.支持参数复用.支持更换数据库类型,希望能帮到大家,若需支持查出来后转换成实体,可以自行扩展dat ...

  4. 【无私分享:ASP.NET CORE 项目实战(第十四章)】图形验证码的实现

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 很长时间没有来更新博客了,一是,最近有些忙,二是,Core也是一直在摸索中,其实已经完成了一个框架了,并且正在准备在生产环境中 ...

  5. 【Java每日一题】20161223

    package Dec2016; public class Ques1223 { public static void main(String[] args){ Integer obj = Integ ...

  6. python学习笔记(基础三:if else流程判断、while循环、for循环)

    if else流程判断 getpass在pycharm中无法使用,在命令行窗口中进入python环境可以使用. import getpassusername = input("usernam ...

  7. swing with transformjs

    Antecedent Facebook made a HTML5 game long time ago. The opening animation is a piece of software th ...

  8. 解决mysql卸载后无法从新安装,卡在最后一步的问题

    mysql服务出现问题往往是最麻烦的,往往需要重装,而重装很多人卸不干净残留文件,更加装不上.在下就遇到这个问题.重装mysql到最后一步时卡在了最后一步的第二条上 解决办法就是卸载后删注册表+删数据 ...

  9. 学习笔记 UpdateXml() MYSQL显错注入

    在学习之前,需要先了解 UpdateXml() . UPDATEXML (XML_document, XPath_string, new_value); 第一个参数:XML_document是Stri ...

  10. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式

    [1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...