数据库
首页 > 数据库> > SQL12 获取每个部门中当前员工薪水最高的相关信息

SQL12 获取每个部门中当前员工薪水最高的相关信息

作者:互联网

描述

有一个员工表dept_emp简况如下:

 

 有一个薪水表salaries简况如下:

 

 获取每个部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary,按照部门编号dept_no升序排列,以上例子输出如下:

 

 

SELECT d1.dept_no, d1.emp_no, s1.salary
FROM dept_emp as d1
INNER JOIN salaries as s1
ON d1.emp_no=s1.emp_no
AND d1.to_date='9999-01-01'
AND s1.to_date='9999-01-01'
WHERE s1.salary in (SELECT MAX(s2.salary)
FROM dept_emp as d2
INNER JOIN salaries as s2
ON d2.emp_no=s2.emp_no
AND d2.to_date='9999-01-01'
AND s2.to_date='9999-01-01'
AND d2.dept_no = d1.dept_no
)
ORDER BY d1.dept_no;

 

标签:01,no,薪水,s1,员工,SQL12,dept,emp,d1
来源: https://www.cnblogs.com/dtx123/p/15359574.html