Mysql_案例2:查询出哪些员工的薪资在部门平均薪资之上

2019/04/09 21:05
阅读数 124

案例:查询出哪些员工的薪资在部门平均薪资之上

1、背景:当前数据库有employee表和department表,数据分别如下:

  employee表:

 

 department表:

 SQL 语句:

 1 SELECT tt.*,d.name as department_name
 2 FROM
 3         (
 4         SELECT e.*,t.avgsal
 5         from 
 6                 (
 7                 SELECT departmentid,AVG(salary) as avgsal
 8                 from employee
 9                 GROUP BY departmentid ) t
10 
11         INNER JOIN
12                             employee as e
13         on 
14                             t.departmentid=e.departmentid
15         WHERE salary > t.avgsal) tt
16 INNER JOIN
17                     department as d
18 on 
19                 tt.departmentid = d.id;

图例解释:

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部