Mysql_案例2:查詢出哪些員工的薪資在部門平均薪資之上
阿新 • • 發佈:2019-04-10
平均薪資 薪資 info png join pan image ron 分享圖片
案例:查詢出哪些員工的薪資在部門平均薪資之上
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) asavgsal 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 17department as d 18 on 19 tt.departmentid = d.id;
圖例解釋:
Mysql_案例2:查詢出哪些員工的薪資在部門平均薪資之上