mysql之部门工资前三的所有员工

题目叙述

mysql之部门工资前三的所有员工_第1张图片

要求

mysql之部门工资前三的所有员工_第2张图片
解法一:
注:mysql也可以用开窗函数,用法与hive略有区别

select d.name department,a.name name,a.salary from department d,(SELECT 
name,salary ,departmentid,
Dense_rank() over(partition by departmentid order by salary desc) as ranking from  employee1 ) a where a.departmentid = d.id and a.ranking <= 3

此方法最为简单,看着不复杂,在此不做详细解释
解法二:自定义变量
简化问题(分步骤)
分解步骤的思路,可以依据必要存在的步骤进行分解
1.根据 部门 (升),薪水 (降) 顺序查询出每个部门的员工 (Department, Employee, Salary)

SELECT dep.Name Department, emp.Name Employee, emp.Salary
FROM Employee emp
INNER JOIN Department dep ON emp.DepartmentId = dep.Id
ORDER BY emp.DepartmentId, emp.Salary DESC

2.每个部门的员工根据薪水进行排序

由于原本没有排序的字段,所以这里就需要自定义变量补充一个字段出来

## 先(部门,薪水)去重,再 部门(升),薪水(降) 排序
SELECT te.DepartmentId, te.Salary,
       CASE 
            WHEN @pre = DepartmentId THEN @rank:= @rank + 1
            WHEN @pre := DepartmentId THEN @rank:= 1
       END AS RANK
FROM (SELECT @pre:=null, @rank:=0)tt,
     (## (部门,薪水)去重,根据 部门(升),薪水(降) 排序
         SELECT DepartmentId,Salary
         FROM Employee
         GROUP BY DepartmentId,Salary
         ORDER BY DepartmentId,Salary DESC
     )te

组合步骤
组合步骤时,尽量将每个步骤变成一个 结果集(不存在二次查询)
再将所有步骤的 结果集进行关联,从而提高性能

SELECT dep.Name Department, emp.Name Employee, emp.Salary
FROM (## 自定义变量RANK, 查找出 每个部门工资前三的排名
        SELECT te.DepartmentId, te.Salary,
               CASE 
                    WHEN @pre = DepartmentId THEN @rank:= @rank + 1
                    WHEN @pre := DepartmentId THEN @rank:= 1
               END AS RANK
        FROM (SELECT @pre:=null, @rank:=0)tt,
             (## (部门,薪水)去重,根据 部门(升),薪水(降) 排序
                 SELECT DepartmentId,Salary
                 FROM Employee
                 GROUP BY DepartmentId,Salary
                 ORDER BY DepartmentId,Salary DESC
             )te
       )t
INNER JOIN Department dep ON t.DepartmentId = dep.Id
INNER JOIN Employee emp ON t.DepartmentId = emp.DepartmentId and t.Salary = emp.Salary and t.RANK <= 3
ORDER BY t.DepartmentId, t.Salary DESC ## t 结果集已有序,根据该集合排序

你可能感兴趣的:(面试题,sql)