Leetcode每日一题:176.second-highest-salary(第二高的薪水)

在这里插入图片描述

在这里插入图片描述

1.解法一:利用 limit 进行限制 此方法可适用于求第N高的薪水,且数据越复杂,速度优势越明显;

limit 的用法为:select * from tableName limit i,n
  tableName:表名
  i:为查询结果的索引值(默认从0开始),当i=0时可省略i 
  n:为查询结果返回的数量
  i与n之间使用英文逗号","隔开

代码为:

select
(select distinct salary from employee 
order by salary desc limit 1,1) 
as SecondHighestSalary

2.解法二:去掉最高薪水后,再取最高薪水;
在小于最高薪水的所有值里面找最高的,自然得到的就是总数据第二高的!!!

select distinct max(salary) as SecondHighestSalary 
from employee 
where salary < (select max(salary) from employee)

猜你喜欢

转载自blog.csdn.net/wyll19980812/article/details/108177794