1.左连接 salaries 和 empoyees,因为后面要用到 hire_date 字段;
2.用 with as 将连接好的表存为临时视图,减少代码量,方便后期查询;
3.select 出 hire_date 和 from_date 相等的记录,即 salary 调整前的记录,as a;
4.select 出 hire_date 和 from_date 不相等的记录,即 salary 调整后的记录,as b;
5.将 a 和 b 做 innner join,用 b.salary-a.salary,即是薪水涨幅,as growth;
6.题目要求是在职员工,所以 where 条件为 to_date='9999-01-01',切记,是 b 表的 to_date
with t as (select salaries.emp_no ,salary ,hire_date ,from_date ,to_date from salaries left join employees on salaries.emp_no=employees.emp_no) select a.emp_no ,(b.salary-a.salary) as growth from (select emp_no ,salary ,to_date from t where hire_date=from_date) a inner join (select emp_no ,salary ,to_date from t where hire_date<>from_date) b on a.emp_no=b.emp_no where b.to_date='9999-01-01' order by growth