LeetCode力扣刷题数据库(177):第n高的薪水

题目

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

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

在这里插入图片描述

分析

1.去重
2.排序
3.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 1 offset n
  );
END
发布了505 篇原创文章 · 获赞 586 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_35456045/article/details/104422751