Employee 表:
Column Name | Type |
---|
id | int |
salary | int |
id 是这个表的主键。
表的每一行包含员工的工资信息。
问题
编写一个 SQL 查询,获取并返回 Employee 表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回 null 。
示例
示例 1:
输入:
Employee 表:
输出:
示例 2:
输入:
Employee 表:
输出:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/second-highest-salary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
注意distinct 去重
法一:ifnull()
select ifnull(
(select distinct salary
from employee
order by salary desc
limit 1,1
)
,null) as SecondHighestSalary
法二:子查询如果查询不到,就会返回null
select
(select distinct salary
from employee
order by salary desc
limit 1,1
) SecondHighestSalary