题解 | #获取每个部门中当前员工薪水最高的相关信息#
获取每个部门中当前员工薪水最高的相关信息
https://www.nowcoder.com/practice/4a052e3e1df5435880d4353eb18a91c6
思路:
两表联结,子表中选取部门ID与max薪水,之后在总表中匹配对应数据输出
SELECT
dept_emp.dept_no,
dept_emp.emp_no,
salaries.salary
FROM
dept_emp
LEFT JOIN salaries ON dept_emp.emp_no = salaries.emp_no
WHERE
(dept_no,salary) IN (
SELECT
dept_no,MAX(salary)
FROM
(
SELECT
dept_emp.dept_no,
dept_emp.emp_no,
salaries.salary
FROM
dept_emp
LEFT JOIN salaries ON dept_emp.emp_no = salaries.emp_no
) AS T
GROUP BY
dept_no
)
ORDER BY
dept_no