【leetCode】【SQL】176.选第二高薪水

176. Second Highest Salary

#题目:
Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.

#题解:

方法一:
select MAX(Salary) as SecondHighestSalary

from Employee
where Salary<(
    select MAX(Salary)
    from Employee
)

方法二:
select case
when count(Salary) >=1 then(
    select distinct Salary
    from Employee
    order by Salary desc
    limit 1,1)
else null
end as NthSalary
from Employee


#题释:

严谨写法:
SELECT IFNULL( (SELECTdistinct Salary as SecondHighestSalary FROM Employee orderby Salary desc limit 1,1) ,null);

SQL之limit用法
mysql支持limit
select * from tablename limit 0,1
即取出第一条记录。

select * from tablename limit 1,1
第二条记录

select * from tablename limit 10,20
从第11条到31条(共计20条)
注意mysql语法的IFNULL关键字的用法:

MYSQL IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。

--------------------- 
作者:wx-泡 
来源:CSDN 
原文:https://blog.csdn.net/qq_16468937/article/details/50780678 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值