码迷,mamicode.com
首页 > 数据库 > 详细

题目2:MySQL----------Second Highest Salary

时间:2015-05-18 09:02:06      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:database   leetcode   sql   mysql   

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.

题目答案:
# Write your MySQL query statement below
SELECT max(Salary) FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee)

题目解析:

SELECT max(Salary) FROM Employee WHERE Salary < (SELECT max(Salary) FROM Employee)

Using max() will return a NULL if the value doesn‘t exist. So there is no need to UNION a NULL. Of course, if the second highest value is guaranteed to exist, using LIMIT 1,1 will be the best answer.







题目2:MySQL----------Second Highest Salary

标签:database   leetcode   sql   mysql   

原文地址:http://blog.csdn.net/chenxun_2010/article/details/45806223

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!