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)的更多相关文章

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

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

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

  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. Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数

    mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...

  6. LeetCode DB: Department Highest Salary

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

  7. [LeetCode#184]Department Highest Salary

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

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

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

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

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

随机推荐

  1. 服务器 'xxxx' 已被定义为分发服务器。若要将该服务器重新配置为分发服务器,必须首先卸载现有的分发服务

    使用AWS DMS(Database Migration Service)将SQL Server数据库同步到AWS的Data Lake上,需要在本地源数据库上配置复制,在配置分发向导最后一步时,遇到下 ...

  2. JSF中run项目时候Tomcat8启动不了的一种方法

    把另一个博客内容迁移到这 我的问题是Tomcat是可以启动的 但是run那个jsp的时候 七月 10, 2016 3:14:54 下午 org.apache.tomcat.util.digester. ...

  3. selenium-获取一组数组进行操作(七)

    selenium-获取一组数组进行操作 以  纵横中文网  中获取24小时畅销榜的书单为例 此文仅做 selenium 在自动化测试中怎么获取一组数据进行说明,不做网络爬虫解释 当然,使用爬虫得到本文 ...

  4. C#解析XML 例子二

    <checkResult> <item> <fmId>XX0001</fmId> <fmItemId>20000RT</fmItemI ...

  5. Ubuntu18.04多个版本GCC编译器的切换

    今天make一个程序的时候,发现程序里面使用到了C++17的标准,而我的gcc仍然是4.8,考虑到系统是ubuntu18.04的,所以感觉应该gcc的版本不会这么低. cd到/usr/bin下,使用指 ...

  6. June 8. 2018 Week Week 23rd Friday

    You'll have bad times, but it'll always wake you up to the good stuff you weren't paying attention t ...

  7. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

  8. Java中使用elasticsearch搜索引擎实现简单查询、修改等操作-已在项目中实际应用

    以下的操作环境为:jdk:1.8:elasticsearch:5.2.0 maven架包下载坐标为: <dependency> <groupId>org.elasticsear ...

  9. 《生命》第一集:Chanllenge of Life (生命的挑战)

    开篇讲的是弗罗里达海岸的海豚用尾巴荡起浑水圈批量捕鱼,肯尼亚的猎豹用集体战术捕猎体积大于自己的鸵鸟,马达加斯加的变色龙用伪装的方式来偷袭昆虫,紧接着讲的是南极洲的食蟹海豹如何从天敌虎鲸的围捕中惊险逃脱 ...

  10. python的shelve模块

    shelve shelve是一额简单的数据存储方案,他只有一个函数就是open(),这个函数接收一个参数就是文件名,并且文件名必须是.bat类型的.然后返回一个shelf对象,你可以用他来存储东西,就 ...