根据题意将需求拆解为3个子需求:
- 找出员工的相关薪水信息;
- 找出经理的相关薪水信息;
- 根据条件,找出员工薪水大于经理薪水的相关信息。
第一步,找出员工的相关薪水信息
select de.dept_no, # 员工所在部门 de.emp_no, # 员工编号 s.salary as emp_salary # 员工薪水 from dept_emp as de join salaries as s on de.emp_no = s.emp_no where de.emp_no not in (select # 该条件选出不是经理的员工 emp_no from dept_manager )
第二步,找出经理的相关信息
select dm.dept_no, # 经理所在部门 dm.emp_no, # 经理的员工编号 s.salary as manager_salary # 经理对应的薪水 from dept_manager as dm join salaries as s on dm.emp_no = s.emp_no
第三步,获取结果,即将第一步获得的结果与第二步获得的结果进行表拼接,再将员工薪水大于经理薪水作为筛选条件,即获得最终结果。
select t1.emp_no, t2.manager_no, t1.emp_salary, t2.manager_salary from ( select de.dept_no, de.emp_no, s.salary as emp_salary from dept_emp as de join salaries as s on de.emp_no = s.emp_no where de.emp_no not in (select emp_no from dept_manager )) as t1 join (select dm.dept_no, dm.emp_no as manager_no, s.salary as manager_salary from dept_manager as dm join salaries as s on dm.emp_no = s.emp_no) as t2 on t1.dept_no = t2.dept_no where t1.emp_salary > t2.manager_salary