0
点赞
收藏
分享

微信扫一扫

Leetcode No.177 第N高的薪水SQL四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)

舍予兄 2022-02-05 阅读 42


一、题目描述

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

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

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

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

二、解题思路

使用DENSE_RANK连续排名函数

SQL四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)

三、代码

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
RETURN (
# Write your MySQL query statement below.
select distinct(salary)
from(
select salary,dense_rank()over(order by Salary desc) as rn
from Employee
)a
where a.rn=N
);
END


举报

相关推荐

0 条评论