[理解leetcode解法]176. Second Highest Salary
2016-03-02 15:58
405 查看
176. Second Highest Salary
#题目:Write a SQL query to get the second highest salary from the
Employeetable.
+----+--------+ | 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); |
mysql支持limit |
注意mysql语法的IFNULL关键字的用法: MYSQL IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。 |
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解