with b as ( select distinct emp_no from salaries where to_date = '9999-01-01' ), a as ( select b.emp_no,salary,rank() over(partition by b.emp_no order by from_date) as rk from b left join salaries on b.emp_no = salaries.emp_no ), c as ( select emp_no,max(rk) as maxrk,min(...