177. Nth Highest Salary

Write a SQL query to get the nth highest salary from the Employee table.

+—-+——–+
| Id | Salary |
+—-+——–+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+—-+——–+

For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.

+————————+
| getNthHighestSalary(2) |
+————————+
| 200 |
+————————+



解答:和上一题查询第二高薪是一个套路,取第N个数就可以了,需要注意的是OFFSET是传入的参数,需要减去1,SQL语句中不能直接运算,要先定义一个局部变量,将偏移先减去1:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N=N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT (SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT N,1) AS result
  );
END

总结,对空集合继续进行查询,或者函数运算操作,返回NULL