查询出部门中所有人的平均薪资等级
案例:查询出部门中所有人的平均薪资等级
背景:当前数据库的department表、salgrade表、employee表,数据分别如下:
-- 查询出每个部门的平均薪资,起别名t ,用作临时表(一会放入子查询),根据部门id进行排序;
SELECT
e.departmentid,AVG(e.salary) as avgsal
FROM
employee as e
GROUP BY
departmentid;
-- 用查询出来的每个部门平均薪资表t,去匹配薪资等级表,找出每个部门的平均薪资是属于什么薪资等级
SELECT
t.departmentid,s.grade
FROM
(SELECT
e.departmentid,AVG(e.salary) as avgsal
FROM
employee as e
GROUP BY
e.departmentid) t
INNER JOIN
salgrade s
on
t.avgsal BETWEEN s.losal and s.hisal
ORDER BY
t.departmentid;
原文链接:https://www.cnblogs.com/wodexk/p/10680416.html