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. [51nod1119]机器人走方格V2

    解题关键: 1.此题用dp的方法可以看出,dp矩阵为杨辉三角,通过总结,可以得出 答案的解为$C_{n + m - 2}^{n - 1}$ 2.此题可用组合数学的思想考虑,总的步数一共有$n+m-2$ ...

  2. [51nod1212]最小生成树模板

    解题关键:注意下标 解法一:prim算法 #include<bits/stdc++.h> #define maxv 1002 #define maxm 50002 #define INF ...

  3. js链表操作

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Angular13 Angular2发送PUT请求在后台接收不到参数

    1 问题描述 利用angular2发送PUT请求时,后端接收不到参数 2 问题诊断 前段参数格式问题,后端获取参数的方法不对 3 解决问题 angular前段:将所有参数编程JSON字符串形式 spr ...

  5. 5、提取snp indel 位点

    le final.snp.list | perl -lane '{$a+=1;print "$a\t$F[0]\t$F[1]\t$F[1]"}' | less >snp_si ...

  6. 使用python内置模块os和openpyxl搜索指定文件夹下Excel中的内容

    在指定路径下,搜索Excel文件中包含的指定内容,首先需要遍历指定路径,得到该路径下所有Excel文件的绝对/相对路径:然后读取Excel中内容,将文件中的每个单元格的值与要搜索的内容进行判断(正则比 ...

  7. 阶段2-新手上路\项目-移动物体监控系统\Sprint3-移动监控主系统设计与开发

    移动图像监控系统 去找一些相关开源程序进行移植:百度搜索-linux 移动监控 motion是一套免费开源的移动图像监测程序 前面我们已经使用了很多开源软件,他们的使用方法都是大同小异的 1).先在当 ...

  8. push和commit的区别

    push和commit的区别 git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库.远程库.git commit操作的是本地库,git push操作的是远程库. git c ...

  9. JS使用replace替换字符串中的某段或某个字符

    函数的介绍参考:http://www.w3school.com.cn/jsref/jsref_replace.asp 下列代码将Hello World!中的World替换为Jim <html&g ...

  10. 修改TABLE中的Column的属性

    删除主键名 这个主键名并不是列名,而是主键约束的名字,如果当时设置主键约束时没有制定约束的名字 设置主键的语句:ALTER TABLE P add constraint pk PRIMARY KEY ...