①查询各部门的平均工资
SELECT AVG(salary),department_id FROM employees GROUP BY department_id;
②连接①结果集和employees表,进行筛选
SELECT employee_id,last_name,salary,e.department_id FROM employees e INNER JOIN ( SELECT AVG(salary) ag ,department_id FROM employees GROUP BY department_id )ag_dep ON e.`department_id` = ag_dep.department_id WHERE salary > ag;