select uni.dept_no, uni.emp_no, max_salary.salary from (select d.dept_no, s.emp_no, s.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' ) as uni, /* 部门编号,员工编号,当前薪水 */ (select d.dept_no, max(s.salary) as 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' group by d.dept_no ) as max_salary /* 部门编号,当前最高薪水 */ where uni.salary = max_salary.salary and uni.dept_no = max_salary.dept_no order by uni.dept_no;
一张表记录最高薪水(部门编号,当前最高薪水),一张表记录所有员工的部门及薪水信息(部门编号,员工编号,当前薪水),用部门编号和薪水相等取到最高薪水的员工ID。