题解 | #获取每个部门中当前员工薪水最高的相关信息#
获取每个部门中当前员工薪水最高的相关信息
https://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6
SELECT dept_no,emp_no,salary as maxSalary FROM dept_emp JOIN salaries USING(emp_no) WHERE (dept_no,salary) in ( SELECT dept_no,max(salary) FROM dept_emp JOIN salaries USING(emp_no) GROUP BY dept_no ) ORDER BY dept_no
1、子表找到每个部门中最高的薪资
SELECT dept_no,max(salary) FROM dept_emp JOIN salaries USING(emp_no) GROUP BY dept_no
2、部门对应薪资的行,并对部门排序
WHERE (dept_no,salary) in ( SELECT dept_no,max(salary) FROM dept_emp JOIN salaries USING(emp_no) GROUP BY dept_no ) ORDER BY dept_no
3、完整代码
SELECT dept_no,emp_no,salary as maxSalary FROM dept_emp JOIN salaries USING(emp_no) WHERE (dept_no,salary) in ( SELECT dept_no,max(salary) FROM dept_emp JOIN salaries USING(emp_no) GROUP BY dept_no ) ORDER BY dept_no