leetcode177-Nth Highest Salary(找出第n大的数据)

问题求解:

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.

问题求解:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    declare n1 int;
    set n1=N-1;
  RETURN (
      # Write your MySQL query statement below.
      select Salary from 
      (select distinct Salary from Employee) t 
      order by Salary desc 
      limit n1,1

  );
END

你可能感兴趣的:(数据库)