码疯窝

LeetCode 每日一题 — Nth Highest Salary

2015/01/13 11:02:36    分类: 日志连载    0人评论 次浏览

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.

分析: 这道题目是要求写一个funciton, 求第N高的salary, 参考上面一题 Second Highest Salary, 可以直接用Limit 写出解法.

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  SET N = N - 1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N,1
  );
END
继续查看有关 日志连载的文章

0个访客评论