176. 第二高的薪水

关键词:null,第二高

解法一:max + where max
153 ms

select max(Salary) as SecondHighestSalary
from employee
where Salary < (select max(Salary) from employee);

解法二:distinct + limit + 临时表
139 ms

imit y offset x 分句表示查询结果跳过 x 条数据,读取前 y 条数据

select(
       select distinct Salary 
       from Employee
       order by Salary desc
       limit 1,1
       ) as SecondHighestSalary;

解法三:ifnull(a,b) + 临时表
141 ms

ifnull(a,b)函数解释:
如果value1不是空,结果返回a;
如果value1是空,结果返回b。

select 
ifnull(
        (select distinct Salary 
         from Employee
         order by Salary desc
         limit 1,1), null
       )
       as SecondHighestSalary;
posted @ 2021-03-26 10:09  银谷  阅读(39)  评论(0)    收藏  举报