欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【LeetCode】184.部门最高工资

程序员文章站 2022-07-14 11:57:36
...

184.部门最高工资

Employee表有所有员工。每个员工有 Id,salary department Id 信息。

【LeetCode】184.部门最高工资

用到的表和数据SQL:

Create table If Not Exists Employee (Idint, Name varchar(255), Salary int, DepartmentId int);
Create table If Not Exists Department (Idint, Name varchar(255));
Truncate table Employee;
insert into Employee (Id, Name, Salary,DepartmentId) values ('1', 'Joe', '70000', '1');
insert into Employee (Id, Name, Salary,DepartmentId) values ('2', 'Henry', '80000', '2');
insert into Employee (Id, Name, Salary,DepartmentId) values ('3', 'Sam', '60000', '2');
insert into Employee (Id, Name, Salary,DepartmentId) values ('4', 'Max', '90000', '1');
Truncate table Department;
insert into Department (Id, Name) values('1', 'IT');
insert into Department (Id, Name) values('2', 'Sales');

答案:

首先让两个表内连接,然后将结果表需要的列都标明,然后就是要找最高的薪水,我们用Max关键字来实现。

select d.Name Department ,e1.Name Employee,e1.Salary Salary 
from Employee e1
inner join Department d on e1.DepartmentId= d.Id
wheree1.Salary in (select Max(e2.Salary) from Employee e2 where e2.DepartmentId =e1.DepartmentId)

写的比较粗糙,有不理解的可以扫描二维码加QQ群找我解答。

【LeetCode】184.部门最高工资