当前位置: 移动技术网 > IT编程>数据库>Mysql > [LeetCode] Nth Highest Salary 第N高薪水

[LeetCode] Nth Highest Salary 第N高薪水

2019年06月21日  | 移动技术网IT编程  | 我要评论

题目
编写一个 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 (
      select distinct salary from employee group by salary
      order by salary desc limit 1 offset n
  );
end

摘自:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网