题解 | #获取每个部门中当前员工薪水最高的相关信息#
获取每个部门中当前员工薪水最高的相关信息
http://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6
使用多列子查询联合dept_no和max(salary)锁定员工信息,可适配一个部门多个员工并列最大薪资的情况
select d1.dept_no, d1.emp_no, s1.salary
from dept_emp d1, salaries s1
where d1.emp_no = s1.emp_no
and d1.to_date > sysdate()
and s1.to_date > sysdate()
and (d1.dept_no, s1.salary) in (
select d2.dept_no, max(s2.salary)
from dept_emp d2, salaries s2
where d2.emp_no = s2.emp_no
and d1.to_date > sysdate()
and s1.to_date > sysdate()
group by d2.dept_no
)
order by d1.dept_no;
也可使用相关子查询
select d1.dept_no, d1.emp_no, s1.salary
from dept_emp d1, salaries s1
where d1.emp_no = s1.emp_no
and d1.to_date > sysdate()
and s1.to_date > sysdate()
and s1.salary = (
select max(s2.salary)
from dept_emp d2, salaries s2
where d2.emp_no = s2.emp_no
and d2.dept_no = d1.dept_no
and d1.to_date > sysdate()
and s1.to_date > sysdate()
group by d2.dept_no
)
order by d1.dept_no;