LeetCode database 之 Second Highest Salary(第二高薪水)
2015-09-05 15:54
411 查看
题目:
Write a SQL query to get the second highest salary from theEmployeetable.
+----+--------+ | 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.
如果没有第二高薪水,则返回NULL
代码:
使用max,如果不存在最高的则返回NULL# Write your MySQL query statement below
SELECT max(Salary) FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee);
使用MySQL的特有函数IFNULL()
# Write your MySQL query statement below
SELECT IFNULL( (SELECT distinct Salary as SecondHighestSalary FROM Employee order by Salary desc limit 1,1) ,null);
相关文章推荐
- 数据库重要概念解释
- Effective C++——条款22(第4章)
- 【NOIP2006】第三题·Jam计数法
- windows下 matlab快速启动【-nojvm】
- sharepoint 2016 学习系列篇(4)-如何部署sharepoint server 2016(3)-Sharepoint服务器安装和部署
- 九度OJ 题目1118:数制转换
- Windows Azure Active Directory (4) China Azure AD Self Password Reset
- 【小技巧】更改终端的字体颜色和背景
- sharepoint 2016 学习系列篇(3)-如何部署sharepoint server 2016(2)-数据库服务器安装和部署
- Processing 教程(6) - 计时器、三角函数
- 云服务---IAAS,PAAS,SAAS
- Arduino 9g舵机操作
- 条款20:宁以 pass-by-reference to const 替换 pass by value
- poj 1759 Garland (二分搜索之其他)
- java 练习
- lucene
- Android viewpager 切换adapter内存溢出的问题
- 【NOIP2005】第三题·采药
- 2015 Google hosts 持续更新【更新于:2015-09-01】
- Nginx+PHP-FPM优化技巧总结