题解 | #获取每个部门中当前员工薪水最高的相关信息#
获取每个部门中当前员工薪水最高的相关信息
https://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6
我的思路:首先合并两张表才能按照部门分组求最高工资,求出每个部门的最高工资(new_table)之后比较难的是获取对应的员工编号.
神奇的是,这个“对应”的过程需要合并三张表: new_table的最高工资需要与salaries中的工资做匹配(内连接),new_table的部门编号需要与dept_emp中的部门编号匹配,dept_emp与salaries中的员工编号需要匹配,闭环了。
select new_table.dept_no,s1.emp_no,new_table.maxSalary
from salaries as s1 inner join
(select d.dept_no,max(s.salary) as maxSalary
from dept_emp as d right join salaries as s
on d.emp_no=s.emp_no
group by d.dept_no) as new_table
on s1.salary=new_table.maxSalary
inner join dept_emp as d2
on new_table.dept_no=d2.dept_no
and s1.emp_no=d2.emp_no
order by new_table.dept_no asc;
from salaries as s1 inner join
(select d.dept_no,max(s.salary) as maxSalary
from dept_emp as d right join salaries as s
on d.emp_no=s.emp_no
group by d.dept_no) as new_table
on s1.salary=new_table.maxSalary
inner join dept_emp as d2
on new_table.dept_no=d2.dept_no
and s1.emp_no=d2.emp_no
order by new_table.dept_no asc;
汤臣倍健公司福利 677人发布