在第二个查询中获取多个记录

问题描述 投票:0回答:1
select max(salary)
from employees 
group by department_id

select * from employees
where salary in (select max(salary)
                 from employees
                 group by department_id  )
sql oracle greatest-n-per-group
1个回答
0
投票

我认为您正在寻找相关的子查询:

select e.*
from employees e
where e.salary = (
    select max(e1.salary)
    from employees e1
    where e1.department_id = e.department_id
)

此查询为您提供所在部门中薪水最高的员工(包括联系)。

出于性能考虑,请考虑使用employees(department_id, salary)上的索引。

© www.soinside.com 2019 - 2024. All rights reserved.