LeetCode: 177. Nth Highest Salary

LeetCode: 177. Nth Highest Salary

题目描述

Write a SQL query to get the nth highest salary from the Employee table.

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

For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.

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

解题思路

将员工表按照薪水从高到低排序,取第 n 行数据。

AC 代码

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN DECLARE TMP INT;
  SET TMP=N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT * FROM (SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC) AS So limit TMP,1 );
END
全部评论

相关推荐

09-13 10:40
门头沟学院 Java
听别人介绍,刷了一堆力扣题,考场上写函数,一直无法通过。赛后才知道要自己写输入输出,力扣害人不浅
Silencer76:输入输出练习题单,请https://www.nowcoder.com/exam/oj?page=1&tab=%E7%AE%97%E6%B3%95%E7%AC%94%E9%9D%A2%E8%AF%95%E7%AF%87&topicId=372
投递中国电信等公司10个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-29 14:02
纠结的茶叶蛋在开会:太简单了,技术和业务没一个够用的,要么学业务,去搞erp或者his,要么专精技术,去搞架构
点赞 评论 收藏
分享
09-11 16:04
门头沟学院 Java
青禾111:面我的小红书面试官傲慢无礼
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务