資料庫SQL實踐25:獲取員工其當前的薪水比其manager當前薪水還高的相關資訊
阿新 • • 發佈:2018-11-16
思想:
題目要求獲取員工其當前的薪水比其manager當前薪水還高的相關資訊,當前表示to_date='9999-01-01',
結果第一列給出員工的emp_no,
第二列給出其manager的manager_no,
第三列給出該員工當前的薪水emp_salary,
第四列給該員工對應的manager當前的薪水manager_salary
首先通過條件dept_emp d inner join dept_manager e on d.dept_no=e.dept_no and d.to_date='9999-01-01' 將dept_emp和dept_manager內連線,即找到每個員工的manager
其次通過條件inner join salaries s1 on d.emp_no = s1.emp_no and s1.to_date='9999-01-01'找出所有員工的當前工資
然後通過條件inner join salaries s2 on e.emp_no = s2.emp_no and s2.to_date='9999-01-01'找出manager的當前工資
最後通過條件where s1.salary>s2.salary找出員工工資比manager工資多的員工
select d.emp_no,e.emp_no as manager_no,s1.salary as emp_salary,s2.salary as manager_salary from dept_emp d inner join dept_manager e on d.dept_no=e.dept_no and d.to_date='9999-01-01' inner join salaries s1 on d.emp_no = s1.emp_no and s1.to_date='9999-01-01' inner join salaries s2 on e.emp_no = s2.emp_no and s2.to_date='9999-01-01' where s1.salary>s2.salary;
內連線的結果集需要很好的掌握