1. 程式人生 > >Mysql_案例2:查詢出哪些員工的薪資在部門平均薪資之上

Mysql_案例2:查詢出哪些員工的薪資在部門平均薪資之上

平均薪資 薪資 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) 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;

圖例解釋:

技術分享圖片

Mysql_案例2:查詢出哪些員工的薪資在部門平均薪資之上