gpt4 book ai didi

mysql - 缩短嵌套 SQL 查询

转载 作者:可可西里 更新时间:2023-11-01 08:07:41 24 4
gpt4 key购买 nike

列出工资高于给定员工所在部门平均工资的员工姓名。

我提出了以下解决方案:名为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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com