我的原始答案:
select salary SecondHighestSalary from
( select salary,rownum rn from (select salary from Employee order by salary desc )) where rn=2 ;
这样会导致没有第二高的薪水时(例如只有一行数据)会没有结果,而不是空值
第二次答案:
select max(salary) SecondHighestSalary from
( select salary,rownum rn from (select salary from Employee order by salary desc )) where rn=2 ;
但这样会导致这样的情况会报错:{1,500},{2,500} 会没有结果
于是再改成:
select salary SecondHighestSalary from ( select salary,rownum rn from (select distinct salary from Employee order by salary desc )) where rn=2 ;
看到还有一种答案:
SELECT MAX(Salary) AS SecondHighestSalary FROM Employee
where salary < ( SELECT MAX(Salary) FROM Employee)