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 ...
随机推荐
- JS 窗口加载与定时器笔记
bom浏览器对象模型 bom由一系列相关的对象构成并且每个对象都提供了很多方法属性 bom顶级对象是window bom是浏览器产商在各自浏览器上定义的,兼容性差 wi ...
- WebLogic 省略项目名称
希望 WebLogic 部署的项目,不需要输入项目名,直接通过IP端口访问. 在 WEB-INF 目录下添加文件 weblogic.xml <?xml version="1.0&quo ...
- 在Linux下安装nginx服务器详细教程
首先安装centos的扩展源 yum install epel-release 安装Nginx 方法一: yum install nginx -y 查看版本号,开启nginx,查看进程 nginx – ...
- C#开发笔记之03-为什么选择IsNotXXX方法而不是IsXXX方法?
C#开发笔记概述 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/959 访问. 为什么有时候要选择IsNotXXX方法而 ...
- K均值聚类和DBSCAN介绍
K均值(K-means)聚类 问题定义:给定数据$\vec{x}_1,\vec{x}_2,\cdots,\vec{x}_n$,将它们分到不同的$K$个簇(cluster)中.定义$\vec{c}=(c ...
- 【POJ3071】Football - 状态压缩+期望 DP
Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...
- 更优雅的配置:docker/运维/业务中的环境变量
目录 docker-compose 环境变量 .env 文件 env_file docker stack 不支持基于文件的环境变量 envsubst envsubst.py 1. 使用行内键值对 2. ...
- 要有一颗理财的心 - 读<富爸爸.穷爸爸>
记得工作没多久后的一次加薪的例行谈话.部门经理和我说,不能靠工资过日子,要多想想怎么投资,我的主要财富就是靠投资赚来的.当时第一反应,老板,你不给我加薪找这借口也太牵强了吧.我的收入只有工资,我的工资 ...
- ethtool 设置网卡接收哈希
检查 [root@localhost]# ethtool -n eth1 rx-flow-hash tcp4 TCP over IPV4 flows use these fields for comp ...
- Eclipse导入项目后JSP页面出现报红
Multiple annotations found at this line:- javax.servlet.jsp.JspException cannot be resolved to a typ ...