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. SQL一对多特殊查询,取唯一一条

    主表: 辅表: 一个app对应多个apk,现在要取上线(Apk_Status最大的)的应用 select * from [dbo].[tbl_APP] as app join ( * from tbl ...

  2. .NET生成ICO图标

    using System; using System.Collections.Generic; using System.Web; using System.Drawing; using System ...

  3. 如何从光盘本地安装CentOS 7图形界面(Gnome GUI)

    本例中通过在CentOS 7中修改repo文件,直接从光盘或者ISO镜像文件安装Gnome图形界面(Gnome GUI),从而避免耗时从官网或镜像下载. 1.首先确保光盘或者ISO镜像文件正确连接到客 ...

  4. shell判断网络主机存活

    判断网络主机存活企业面试题4:写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多) #!/bin/sh#[ -f /etc/init.d/functions ] & ...

  5. kafka之c接口常用API------librdkafka

    1 安装方法以及相关库文件 https://github.com/edenhill/librdkafka 2 High-level producer High-level consumer Simpl ...

  6. e.key && e.which && e.keyCode

    官方推荐用e.key来描述状态码,其他两种属性可能会在未来被废弃. 且key,keyCode和which 为只读属性   但是会有浏览器兼容性的问题,可以采用如下代码:   let key = ''; ...

  7. Eclipse插件无法识别的解决方法汇总

    参考 http://www.cnblogs.com/apollolee/archive/2013/06/18/3142243.html

  8. Go语言——没有对象的面向对象编程

    本文译自Steve Francia在OSCON 2014的一个PPT,原作请前往:https://spf13.com/presentation/go-for-object-oriented-progr ...

  9. AOP常用注解

    1.@Aspect 配置切面Bean,和<bean.../>元素进行配置无区别,一样支持依赖注入来配置属性值: 如果启动了Spring的"零配置"特性,一样可以让Spr ...

  10. matlab矩阵与数组

    数组运算:数与数组加减:k+/-A %k加或减A的每个元素数组乘数组: A.*B %对应元素相乘数组乘方: A.^k %A的每个元素k次方:k.^A,分别以k为底A的各元素为指数求幂值数除以数组: k ...