1.查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资
①查询各部门的平均工资
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;