select
t5.dept_no,
t5.emp_no,
t5.salary
from
(
select
dept_no,
max(salary) `salary`
from
(
select
t1.dept_no,
t1.emp_no,
t2.salary
from
(
select
emp_no,
dept_no
from dept_emp
where dept_emp.to_date = '9999-01-01'
) t1
join
(
select
emp_no,
salary
from salaries
where salaries.to_date='9999-01-01'
) t2
on t1.emp_no = t2.emp_no
) t3
group by dept_no
) t4
join
(
select
t1.dept_no,
t1.emp_no,
t2.salary
from
(
select
emp_no,
dept_no
from dept_emp
where dept_emp.to_date = '9999-01-01'
) t1
join
(
select
emp_no,
salary
from salaries
where salaries.to_date='9999-01-01'
) t2
on t1.emp_no = t2.emp_no
) t5
on t4.dept_no = t5.dept_no and t4.salary = t5.salary
order by t5.dept_no;