select dept_no, emp_no, salary maxsalary from ( select dept_no, emp_no, salary, rank()over(partition by dept_no order by salary desc)rk from ( select d.emp_no, dept_no, salary from dept_emp d join salaries s on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date='9999-01-01' )a )b where rk=1 ...