有一个员工表employees简况如下:
有一个薪水表salaries简况如下:
请你查找薪水排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不能使用order by完成,以上例子输出为:
找第二多的人,那么就只有一个人比他薪水高,采用表内连接再计数即可
select e.emp_no,s3.salary,e.last_name,e.first_name from employees e inner join( select s1.emp_no,s1.salary from salaries s1 inner join salaries s2 on s1.salary < s2.salary where s1.to_date = '9999-01-01' and s2.to_date = '9999-01-01' group by s1.emp_no having count(*) = 1 ) s3 on e.emp_no = s3.emp_no;