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
.
题意:查询第n高的记录,如果没有请返回null
思路:按照salary排正序,使用limit挑选出第N高
最终sql:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
declare M INT;
set M=N-1;
RETURN (
select DISTINCT SALARY from Employee order by SALARY DESC LIMIT M,1
);
END