列出工资高于给定员工所在部门平均工资的员工姓名。
我提出了以下解决方案: 名为employee的表中的列名是:emp_id、name、dept、salary。
select name from employee
where salary >
(
select avg(salary) from employee
where dept= (select dept from employee where emp_id = 'a10')
)
and
dept = (select dept from employee where emp_id = 'a10')
;
我可以将这个查询缩短一点,并期望得到相同的结果吗?
最佳答案
这个查询是特定于 Oracle 的,但它的优点是只访问 employee
表一次:
select name from
(select name,
salary,
avg(salary) over (partition by dept) as avg_salary
from employee)
where salary > avg_salary;
关于mysql - 缩短嵌套 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7489433/