select *
from employees
where department_id,salary in (
select department_id,max(salary)
from employees group by department_id
)
您想进行元组比较-您需要在in
左侧的列元组周围加上括号:
select *
from employees
where (department_id,salary) in (
select department_id, max(salary) from employees group by department_id
)
请注意,可以使用窗口函数更有效地表达此前1组查询:
select *
from (
select e.*, rank() over(partition by department_id order by salary desc nulls last) rn
from employees e
) t
where rn = 1