通知
关于网站更多信息请加whimurmur模板/jpress插件QQ群(1061691290)            网站从因情语写改为晴雨            这个网站的模板也从calmlog_ex改为 whimurmur

leetcode数据库 系列题 部门最高工资问题

542人浏览 / 0人评论 / | 作者:因情语写  | 分类: 数据库  | 标签: 数据库  /  leetcode  | 

作者:因情语写

链接:https://www.proprogrammar.com/article/631

声明:请尊重原作者的劳动,如需转载请注明出处


    184. 部门工资最高的员工

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  |
+------------+----------+--------+
select d.Name Department, e.Name Employee, maxSalEmp.Salary
from Employee e, Department d,
(select DepartmentId, max(e2.Salary) Salary
from Employee e2
group by e2.DepartmentId) maxSalEmp
where d.Id = e.DepartmentId and e.DepartmentId = maxSalEmp.DepartmentId and e.Salary = maxSalEmp.Salary;

    学习group by + 聚集函数求分组内的max值,三表连接

    再看另一种较快的解法

select d.name as Department,a.name as Employee,a.Salary as Salary from Employee a inner join Department d on d.id=a.DepartmentId
where (a.salary,a.DepartmentId) in (select max(salary),DepartmentId from Employee e group by DepartmentId)

    学习了inner join..on的用法,与两字段in的用法(第一次知道可以多字段in)

    185. 部门工资前三高的所有员工

Employee 表包含所有员工信息,每个员工有其对应的工号 Id,姓名 Name,工资 Salary 和部门编号 DepartmentId 。

+----+-------+--------+--------------+
| Id | Name  | Salary | DepartmentId |
+----+-------+--------+--------------+
| 1  | Joe   | 85000  | 1            |
| 2  | Henry | 80000  | 2            |
| 3  | Sam   | 60000  | 2            |
| 4  | Max   | 90000  | 1            |
| 5  | Janet | 69000  | 1            |
| 6  | Randy | 85000  | 1            |
| 7  | Will  | 70000  | 1            |
+----+-------+--------+--------------+

Department 表包含公司所有部门的信息。

+----+----------+
| Id | Name     |
+----+----------+
| 1  | IT       |
| 2  | Sales    |
+----+----------+

编写一个 SQL 查询,找出每个部门获得前三高工资的所有员工。例如,根据上述给定的表,查询结果应返回:

+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT         | Max      | 90000  |
| IT         | Randy    | 85000  |
| IT         | Joe      | 85000  |
| IT         | Will     | 70000  |
| Sales      | Henry    | 80000  |
| Sales      | Sam      | 60000  |
+------------+----------+--------+

解释:

IT 部门中,Max 获得了最高的工资,Randy 和 Joe 都拿到了第二高的工资,Will 的工资排第三。销售部门(Sales)只有两名员工,Henry 的工资最高,Sam 的工资排第二。

 

select d.Name Department, e.Name Employee, e.Salary
from Employee e, Department d
where e.DepartmentId = d.Id
and (
        select count(distinct e2.Salary) from Employee e2 where e2.Salary > e.Salary and e2.DepartmentId = e.DepartmentId
    ) < 3
order by e.DepartmentId, e.Salary desc;

    学习了同一个表关联查询的知识,e1,e2相同表关联查询,查出高于某工资的不同工资小于3个的,说明某工资可能为最高,第二高,第三高,即前三的不同工资

    再看另一种比较快的

select d.name as department,m.name as employee,salary
from 
(select departmentid,Name,salary,
if(@pre_d=departmentid,if(@pre_s=salary,@cur_rank,@cur_rank:=@cur_rank+1),@cur_rank:=1) as ranking,
@pre_d:=departmentid,@pre_s:=salary
from employee,(select @pre_d:=null,@pre_s:=null,@cur_rank:=0) r 
order by departmentid,salary desc) m join Department d on m.departmentid=d.id and m.ranking<=3

    学习了变量的使用:在from中定义变量,在select中使用变量,还有对于复杂的判断,使用嵌套的if,多字段的order by及默认升序,使用desc降序,join等同inner join,也等同于用逗号","


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!
广告位-帮帮忙点下广告