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, t.Salary AS Salary FROM Employee e join
(Select DepartmentId, MAX(Salary) AS Salary from Employee group BY departmentId) t
USING (DepartmentId,Salary)
JOIN Department d
on t.DepartmentId = d.Id;

Department Highest Salary的更多相关文章

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

  2. [LeetCode] Department Highest Salary -- 数据库知识(mysql)

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

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

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

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

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

  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. 【SQL】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#184]Department Highest Salary

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

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

  9. LeetCode - Department Highest Salary

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

随机推荐

  1. Java探索之旅(6)——对象和类

    1.知识要点 假设: public ClassName{     int data;   String name;     ClassName(){data=1;}     public static ...

  2. [dp]最长单调递增子序列LIS

    https://www.51nod.com/tutorial/course.html#!courseId=12 解题关键: 如果将子序列按照长度由短到长排列,将他们的最大元素放在一起,形成新序列$B\ ...

  3. hibernate&nbsp;hql&nbsp;查询指定…

    以数组的形式抛出,前台页面就要把它当成一个数组来处理 以对象抛出,就要当成一个对象来处理. 在JSP页面使用标签时一定要注意这点. 版权声明:本文为博主原创文章,未经博主允许不得转载.

  4. 24.command-executor

    这里先给出题目链接: https://command-executor.hackme.inndy.tw/ 这是一道不错的ctf题,首先说一下考察点: 文件包含读源码 代码分析结合CVE CVE导致的命 ...

  5. 牛客网小白月赛6H(最小生成树【克鲁斯卡尔】)

    #include<bits/stdc++.h>using namespace std;long long sum=0;struct node{    int a,b,len;}road[5 ...

  6. CodeForces 137C【贪心+优先队列】

    这种区间的贪心好像都出"烂"了? 不过还是想写一下... 先按照区间左端点排序一下,然后搞个优先队列维护当前最小的右端点. #include <bits/stdc++.h&g ...

  7. 数据绑定—Source(绑定到静态类的静态属性)

    <UserControl x:Class="绑定.绑定Source" xmlns="http://schemas.microsoft.com/winfx/2006/ ...

  8. 洛谷P4707 重返现世(扩展MinMax容斥+dp)

    传送门 我永远讨厌\(dp.jpg\) 前置姿势 扩展\(Min-Max\)容斥 题解 看纳尔博客去→_→ 咱现在还没搞懂为啥初值要设为\(-1\)-- //minamoto #include< ...

  9. Unity---MonoBehaviour9大生命周期

    1.MonoBehaviour9大生命周期 MonoBehaviour是一个基类,所有Unity脚本都派生自该类. Awake():在脚本实例化时被调用. Start():在Awake之后,Updat ...

  10. 【Kafka】《Kafka权威指南》入门

    发布与订阅消息系统 在正式讨论Apache Kafka (以下简称Kafka)之前,先来了解发布与订阅消息系统的概念, 并认识这个系统的重要性.数据(消息)的发送者(发布者)不会直接把消息发送给接收 ...