码迷,mamicode.com
首页 > 数据库 > 详细

Mysql案例6_求平均薪资的等级最低的部门的部门名称

时间:2019-04-14 09:40:50      阅读:478      评论:0      收藏:0      [点我收藏+]

标签:code   info   .com   span   部门   img   osal   图片   pre   

背景:当前数据库的department表、salgrade表、employee表,数据分别如下:

技术图片

 

SQL语句:

SELECT 
            t.*,s.grade
FROM 
            (
                    SELECT 
                            e.departmentid, d.`name`,AVG(salary) avgsal
                    FROM 
                            employee e
                    INNER JOIN
                            department d
                    on 
                            e.departmentid=d.id
                    GROUP BY 
                            e.departmentid,d.`name`  ) t
INNER JOIN
            salgrade s 
ON
            t.avgsal BETWEEN s.losal AND s.hisal
WHERE
            grade = (SELECT 
                               MIN(t.grade) mingra
                      FROM (
                                                SELECT 
                                                    t.*,s.grade
                                                FROM 
                                                        (
                                                                    SELECT 
                                                                            e.departmentid, d.`name`,AVG(salary) avgsal
                                                                    FROM 
                                                                            employee e
                                                                    INNER JOIN
                                                                            department d
                                                                    on 
                                                                            e.departmentid=d.id
                                                                    GROUP BY 
                                                                            e.departmentid,d.`name`) t
                                                    INNER JOIN
                                                            salgrade s
                                                    ON
                                                            t.avgsal BETWEEN s.losal AND s.hisal) t
                                                )

 

Mysql案例6_求平均薪资的等级最低的部门的部门名称

标签:code   info   .com   span   部门   img   osal   图片   pre   

原文地址:https://www.cnblogs.com/wodexk/p/10703865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!