leetcode-database-177. Nth Highest Salary

来源:互联网 发布:软件著作是什么意思 编辑:程序博客网 时间:2024/06/05 08:06

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

0 0
原创粉丝点击