Q200510-01: 求部门工资最高的员工
问题: 求部门工资最高的员工
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
解答:
mysql> select d.name as department,b.name as employee,b.salary from (select e.* from employee e inner join
-> (select max(salary) as max_salary,departmentid from employee group by departmentid) a
-> on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
-> on b.departmentid=d.id ;
+------------+----------+--------+
| department | employee | salary |
+------------+----------+--------+
| Sales | Henry | 80000 |
| IT | Max | 90000 |
+------------+----------+--------+
2 rows in set (0.00 sec)
过程:
create table employee(
id int,
name varchar(20) not null,
salary int,
departmentid int not null,
primary key(id)) insert into employee(id,name,salary,departmentid) values ('','Joe','','');
insert into employee(id,name,salary,departmentid) values ('','Henry','','');
insert into employee(id,name,salary,departmentid) values ('','Sam','','');
insert into employee(id,name,salary,departmentid) values ('','Max','',''); create table department(
id int,
name varchar(20) not null,
primary key(id)) insert into department(id,name) values('','IT');
insert into department(id,name) values('','Sales'); select max(salary) as max_salary,departmentid from employee group by departmentid select e.* from employee e inner join
(select max(salary) as max_salary,departmentid from employee group by departmentid) a
on e.salary=a.max_salary and e.departmentid=a.departmentid select b.id,b.name,b.salary,d.name from (select e.* from employee e inner join
(select max(salary) as max_salary,departmentid from employee group by departmentid) a
on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
on b.departmentid=d.id select d.name as department,b.name as employee,b.salary from (select e.* from employee e inner join
(select max(salary) as max_salary,departmentid from employee group by departmentid) a
on e.salary=a.max_salary and e.departmentid=a.departmentid) b left join department d
on b.departmentid=d.id
--2020年5月10日 17点22分--
1. 部门工资最高的员工
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id。
+----+-------+--------+--------------+
| Id | Name | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
+----+-------+--------+--------------+
Department 表包含公司所有部门的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
编写一个 SQL 查询,找出每个部门工资最高的员工。例如,根据上述给定的表格,Max 在 IT 部门有最高工资,Henry 在 Sales 部门有最高工资。
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
Q200510-01: 求部门工资最高的员工的更多相关文章
- [SQL]LeetCode184. 部门工资最高的员工 | Department Highest Salary
The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...
- leetcode 184 部门工资最高的员工
题目描述:Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. Department 表包含公司所有部门的信息. 编写一个 SQL 查询,找 ...
- mysql查询之部门工资最高的员工
最近发现一个网站 力扣 查看 上面有很多算法和数据库的题目,做了一下,发现自己平时都疏忽了,因此边做边记录下来 Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 de ...
- LeetCode:184.部门工资最高的员工
题目链接:https://leetcode-cn.com/problems/department-highest-salary/ 题目 Employee 表包含所有员工信息,每个员工有其对应的 Id, ...
- sql 184. 部门工资最高的员工
Employee 表包含所有员工信息,每个员工有其对应的 Id, salary 和 department Id. +----+-------+--------+--------------+| Id ...
- sql查询:部门工资前三高的员工和部门工资最高的员工
创建表:Create table If Not Exists Employee (Id int, Name varchar(255), Salary int, DepartmentId int);Cr ...
- 184. 部门工资最高的员工 + join + in
184. 部门工资最高的员工 LeetCode_MySql_184 题目描述 题解分析 1.首先需要使用group by找出工资最高的值 2. 然后考虑到最高工资的可能有多位,所以使用in语句找到所有 ...
- 如何用分析函数找出EMP表中每个部门工资最高的员工
EMP表是Oracle测试账户SCOTT中的一张雇员表,首先,我们来看看emp表的数据 SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE ...
- Oracle求部门员工工资占总工资的比率
--根据每个部门来统计部门工资总和 select deptid, sum(sal) 工资合计 from emp group by deptid; --根据每个部门来统计部门工资总和select dep ...
随机推荐
- C#设计模式之14-命令模式
命令模式(Command Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/413 访问. 命令模式属于行 ...
- C#LeetCode刷题之#263-丑数(Ugly Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3862 访问. 编写一个程序判断给定的数是否为丑数.丑数就是只包含 ...
- LeetCode 861翻转矩阵后得分详细解法
1. 题目内容 有一个二维矩阵 A 其中每个元素的值为 0 或 1 . 移动是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0. 在做出任意次数的移动后 ...
- 01从DataGrid中导入到Excel
01网络上有很多导出数据到Excel的方法,我在网上找到了一种比较简单实用的方法(参考了网友的方法) string fileName = ""; Microsoft.Win32.S ...
- vue_如何判断变量是数组还是对象
一.typeof判断数据类型(判断数组跟对象都返回object) console.log(typeof null); // "object" console.log(typeof ...
- 性能分析(7)- 未利用系统缓存导致 I/O 缓慢案例
性能分析小案例系列,可以通过下面链接查看哦 https://www.cnblogs.com/poloyy/category/1814570.html 前提 前面有学到 Buffer 和 Cache 的 ...
- 实现图像的二值化(java+opencv)
书里的解释: 其他的没找到什么资料,直接参考百度百科 https://baike.baidu.com/item/%E5%9B%BE%E5%83%8F%E4%BA%8C%E5%80%BC%E5%8C%9 ...
- DP搬运工1 [来自yyy--mengbier的预设型dp]
DP搬运工1 题目描述 给你 \(n,K\) ,求有多少个 \(1\) 到 \(n\) 的排列,满足相邻两个数的 \(max\) 的和不超过 \(K\). 输入格式 一行两个整数 \(n,K\). 输 ...
- 2020.5.28 第八篇 Scrum冲刺博客
Team:银河超级无敌舰队 Project:招新通 项目冲刺集合贴:链接 目录 一.每日站立会议 1.1 会议照片 1.2 项目完成情况 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3. ...
- 个人项目wordcount
1.Github项目地址:https://github.com/inimble/wordcount.git 2.估算耗费的时间(如右表) PSP2.1 Personal Software Proces ...