[理解leetcode解法]176. Second Highest Salary
来源:互联网 发布:淘宝卖家骗局大全 编辑:程序博客网 时间:2024/05/29 11:02
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) 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条)
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()返回一个数字或字符串值,取决于它被使用的上下文环境。
0 0
- [理解leetcode解法]176. Second Highest Salary
- leetcode 176. Second Highest Salary
- leetcode 176. Second Highest Salary
- leetcode.176. Second Highest Salary
- leetcode 176. Second Highest Salary
- Leetcode 176. Second Highest Salary
- leetcode 176. Second Highest Salary
- leetcode 176. Second Highest Salary
- 176. Second Highest Salary(Leetcode)
- LeetCode:Second Highest Salary
- Leetcode: Second Highest Salary
- LeetCode Second Highest Salary
- Second Highest Salary Leetcode
- Leetcode: Second Highest Salary
- LeetCode Second Highest Salary
- 【Leetcode】 Second Highest Salary
- leetcode-Second Highest Salary
- 【LeetCode】Second Highest Salary && Nth Highest Salary
- 修改mysql的root密码?
- L GMS预置
- 模态对话框 和 非模态对话框
- windows下搭建React Native for Android 开发环境
- git服务器搭建与客户端配置说明
- [理解leetcode解法]176. Second Highest Salary
- 设计模式 ( 十二 ) 职责链模式(Chain of Responsibility)(对象行为)
- apprtc(webrtc) 本地搭建服务器
- 基于Redis实现分布式锁
- Android中shape中的属性大全
- iOS、Xcode监测键盘的显示和隐藏变化,并获得键盘高度,改变tableView的frame和偏移
- postgresql 9.3.1 编译安装
- centos网络配置实例
- C++第一次作业