力扣177. 第N高的薪水

力扣177. 第N高的薪水

https://leetcode-cn.com/problems/nth-highest-salary/

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

+----+--------+
| Id | Salary |
+----+--------+
| 1  | 100    |
| 2  | 200    |
| 3  | 300    |
+----+--------+
例如上述 Employee 表,n = 2 时,应返回第二高的薪水 200。如果不存在第 n 高的薪水,那么查询应返回 null。

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

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    set n = N-1;
  RETURN 
  (
      # Write your MySQL query statement below.
      select 
      ifnull
      (
          (
          select distinct salary 
          from employee
          order by  salary desc
          limit n,1
          #limit x,y ,是跳过x行,取y行数据的意思,类似于 limit y offset x, 而不是类似于 limit x offset y。 limit x offset y是跳过y行,取x行数据
          ),null
        )
          as NthHighestSalary
  );
END
发布了23 篇原创文章 · 获赞 0 · 访问量 137

猜你喜欢

转载自blog.csdn.net/qq_35683407/article/details/105424683
今日推荐