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 ...
随机推荐
- [转]解决The requested resource is not available的方法
此博文为转载博文,首先感谢原作者 HTTP Status 404(The requested resource is not available)异常主要是路径错误或拼写错误造成的,请按以下步骤逐一排 ...
- java从零到变身爬虫大神
刚开始先从最简单的爬虫逻辑入手 爬虫最简单的解析面真的是这样 import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java. ...
- 2020-04-29:现在你有个秒杀抢购的app,用户不断大量增加,技术层面,你要怎么做
2020-04-29:现在你有个秒杀抢购的app,用户不断大量增加,技术层面,你要怎么做,才能既满足用户需求,又能扛住压力,还能帮公司合理支出?福哥答案2020-04-29: 限流(杀部分用户祭天). ...
- 题解 P1407
建图方式:旧关系女人连男人,现关系男人连女人(当然,反过来也可以) 原因可以这样考虑: 如果一个男的把女的绿了,那么这个女人就会去找一个她曾经交往过的男人,也就是在这种情况下,某种"影响&q ...
- 遍历数组,对象和JSON
遍历数组 var arr2 = [3,4,5,6,7,8]; //第一种方法 for(var i =0;i<arr.length;i++){ console.log(arr2[i]); } // ...
- 封装Vue Element的table表格组件
上周分享了几篇关于React组件封装方面的博文,这周就来分享几篇关于Vue组件封装方面的博文,也好让大家能更好地了解React和Vue在组件封装方面的区别. 在封装Vue组件时,我依旧会交叉使用函数式 ...
- 微信access token过期
两台服务器使用同一个微信账号(同一个app id) 时,当其中一台服务器向微信请求access token时,会造成另一台服务器的access token过期
- sqlalchemy怎么order_by降序/升序并取第一条数据
原文链接:https://blog.csdn.net/mark4541437/article/details/103755721 sqlalchemy怎么order_by降序/升序并取第一条数据 fr ...
- 浅谈Docker(二)
Docker使用cgroup实现CPU,内存和磁盘IO等系统资源的限制. CPU Docker现在有2个与CPU资源相关的参数,-c可以指定CPU的占比,--cpuset可以绑定CPU.例如,指定容器 ...
- Linux下安装mysql时报错:FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:Data::Dumper
如题,安装mysql过程中,执行scripts/mysql_install_db --user=mysql命令时报错: FATAL ERROR: please install the followin ...