ITPub博客

首页 > IT职业 > IT图书 > (LeetCode:数据库)第N高薪水

(LeetCode:数据库)第N高薪水

IT图书 作者:darkfiber 时间:2021-09-09 10:21:58 0 删除 编辑

编写一个 SQL 查询,获取 Employee 表中第 n 高的薪水(Salary)。

+----+--------+| Id | Salary |
+----+--------+
| 1  | 100    || 2  | 200    |
| 3  | 300    |+----+--------+

例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

+------------------------+| getNthHighestSalary(2) |+------------------------+| 200                    |+------------------------+

Solution:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INTBEGIN
    Declare M int;
    Set M = N-1;  RETURN (      # Write your MySQL query statement below.
      select IFNULL((select distinct Salary from Employee order by Salary DESC limit M,1),NULL) AS getNthHighestSalary
  );
END



作者:lconcise
链接:https://www.jianshu.com/p/0e0f5cf21894


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4399/viewspace-2821063/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    93
  • 访问量
    527