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                    |
+------------------------+



Solution:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N=N-1;
  RETURN (
      SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET N
  );
END
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,578评论 0 23
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,199评论 0 10
  • 175. Combine Two Tables 传送门:https://leetcode.com/problems...
    廖少少阅读 4,253评论 0 1
  • 有时候很羡慕别人 我知道,只有自己努力才能成为那样的人 然而,有很多限制 难以突破, 并不喜欢现在的状态,好希望,...
    羽翎儿阅读 1,297评论 0 0
  • 补充一下肖永明董事长的一些背景资料 多家公司背景 青海中浩天然气化工有限公司、青海川蓝钾肥有限公司、西藏中胜矿业有...
    willstar阅读 4,748评论 0 0

友情链接更多精彩内容