Write a SQL query to get the nth highest salary from the Employee table.
+----+--------+| Id | Salary |+----+--------+| 1 | | | 2 | | | 3 | |+----+--------+
For example, given the above Employee table, theNthHighest salary whereN= 2 is200. If there is noNthHighest salary, then the query should returnnull.
Note that the same salary is counted as one, and that limit and offset cannot be used in expressions, at least not when I write.
At first do not know how to declare a variable, the result obstinately wrote this method:
CREATE FUNCTION getnthhighestsalary (N INT) RETURNS intbegin RETURN (select (select A.salary from (select Distinct b.salary from employee B union ALL select Max (c.salary) from employee C) a order by a.salary desc LIMIT 1 Offset N ) ); END
Later, in discuss, I saw how to declare a variable:
CREATE FUNCTION getnthhighestsalary (N INT) RETURNS intbegindeclare m Int;set m = N-1; RETURN ( select distinct salary from Employee order by salary desc LIMIT 1 offset M ); END
Leetcode Nth Highest Salary