177. Nth Highest Salary (第N高的薪水)
题目概述
编写一个SQL查询,获取Employee表中第n高的薪水。
Id | Salary |
1 | 100 |
2 | 200 |
3 | 300 |
例如上述Employee表,n=2时,应返回第二高的薪水200。如果不存在第n高的薪水,那么查询返回null。
getNthHighestSalary(2) |
200 |
思路
该题目是176.Second Highest Salary的升级版本。可以照着这道题的思路去写。
代码实现
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare temp integer; set temp = N-1; RETURN ( # Write your MySQL query statement below. select IFNULL((select distinct Salary from Employee order by Salary desc limit temp,1), null) ); END