select
A.emp_no emp_no,
B.emp_no manager_no,
A.salary emp_salary,
B.salary manager_salary
from
(select
s1.emp_no,
de.dept_no,
s1.salary
from salaries s1
left join dept_emp de
on s1.emp_no=de.emp_no
where s1.to_date='9999-01-01'
and de.to_date='9999-01-01') A
join
(select
s2.emp_no,
dm.dept_no,
s2.salary
from salaries s2
left join dept_manager dm
on s2.emp_no=dm.emp_no
where s2.to_date='9999-01-01'
and dm.to_date='9999-01-01') B
on A.dept_no=B.dept_no
where A.salary>B.salary
#我的思路(参考):
1次表连接dept_emp、salaries,左连接(此处join也可以),获得所有员工对应的当前的薪水,别名A;
2次表连接dept_manager、salaries表连接,左连接(此处join也可以),获得所有经理manager对应的当前的薪水,别名B;
3次表连接,表A、B连接,此时连接键是dept_no-----员工工资表与经理工资表是根据同个部门联结的;
4.员工当前的薪水比其manager当前薪水还高,where A.salary>B.salary
5.最后需要select输出:
A.emp_no emp_no,
B.emp_no manager_no,
A.salary emp_salary,
B.salary manager_salary