题目:
获取员工其当前的薪水比其manager当前薪水还高的相关信息,
第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
解题思路:
第一步:联结dept_manager,salaries表作为临时表t
SELECT dm.dept_no,dm.emp_no,s.salary FROM dept_manager dm INNER JOIN salaries s ON dm.emp_no = s.emp_no WHERE to_date = '9999-01-01';
第二步:联结dept_emp,salaries表获得非经理员工emp_no,salary的表
SELECT de.emp_no,s.salary FROM dept_emp de INNER JOIN salaries s ON de.emp_no = s.emp_no WHERE de.emp_no NOT IN (SELECT emp_no FROM dept_manager) AND WHERE to_date = '9999-01-01';
第三步:联结第一步和第二步,mangaer组合和薪水高低比较作为并列过滤条件
SELECT de.emp_no,t.emp_no AS manager_no,s.salary AS emp_salary,t.salary AS manager_salary FROM dept_emp de INNER JOIN salaries s ON de.emp_no = s.emp_no AND s.to_date = '9999-01-01' LEFT JOIN (SELECT dm.dept_no,dm.emp_no,s.salary FROM dept_manager dm INNER JOIN salaries s ON dm.emp_no = s.emp_no AND s.to_date = '9999-01-01') t ON de.dept_no = t.dept_no WHERE de.emp_no NOT IN (SELECT emp_no FROM dept_manager) AND s.salary > t.salary
如有错误欢迎随时评论区指出,感激不尽~