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

此题相较于Second Highest Salary做了一些改进:

  • 创建mysql function;
  • 需要判断传入参数的合理性.

因此,对代码改动如下所示:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
DECLARE P INT DEFAULT N-1;
IF (P<0) THEN
RETURN NULL;
ELSE
RETURN (
# Write your MySQL query statement below.
SELECT IFNULL(
(
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT P,1)
,NULL)
AS SecondHighestSalary
);
END IF;
END

PS:

如果您觉得我的文章对您有帮助,请关注我的微信公众号,谢谢!

LeetCode——Nth Highest Salary的更多相关文章

  1. [LeetCode] Nth Highest Salary 第N高薪水

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  2. LeetCode - Nth Highest Salary

    题目大概意思是要求找出第n高的Salary,直接写一个Function.作为一个SQL新手我学到了1.SQL中Function的写法和变量的定义,取值.2.limit查询分    页的方法. 在这个题 ...

  3. LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/ Write a SQL query to get the nth highe ...

  4. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  5. find the Nth highest salary(寻找第N高薪水)

    Suppose that you are given the following simple database table called Employee that has 2 columns na ...

  6. [SQL]LeetCode177. 第N高的薪水 | Nth Highest Salary

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  7. 【SQL】177. Nth Highest Salary

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  8. [LeetCode]-DataBase-Nth Highest Salary

    Write a SQL query to get the nth highest salary from the Employee table. +----+--------+ | Id | Sala ...

  9. [LeetCode] Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

随机推荐

  1. ue4 FString 中文乱码问题

    使用FString出现乱码,最简单的情况,FString Str = "你好"; 这时候就会出现乱码,解决方法是改成这样 FString Str = TEXT("你好&q ...

  2. 28.Java基础_抽象类

    抽象类的成员特点 public abstract class Animal { private String name; private int age; public Animal() { } pu ...

  3. 【cf1111】C. Creative Snap (dfs+dp)

    传送门 简单的dfs+dp即可解决.根本不用动态开点 /* * Author: heyuhhh * Created Time: 2019/11/13 10:12:42 */ #include < ...

  4. Vue props中Object和Array设置默认值

    Vue中,在props中设置Object和Array的默认值 seller: { type: Object, default() { return {} } } seller: { type: Obj ...

  5. lua 1 基本语法和注意事项

    笔记总结自: http://www.runoob.com/lua/lua-data-types.html 基本数据类型: 数据类型 描述 nil 这个最简单,只有值nil属于该类,表示一个无效值(在条 ...

  6. java调用含第三方库的py文件

    这是一个心酸的历程. py文件如下: 这里调用出现的问题主要是第三方包的问题,因为你的py文件里可能含有很多三方库文件,jython的jar包里可能不含有这个,所以这时需要你找到你已有三方库文件的ex ...

  7. QTreeWidgetItem清空子节点

    下面列出,xxbs遇到的注意点儿: 1. QTreeWidget::collapseAll(); //xxbs::先折叠所有根项. 如果某个根是展开的,先删除根的子项再折叠,展开的凸显状态角色无法清除 ...

  8. svn merge操作

    使用SVN做Merge操作时,会包含6个选项,下面就这6个选项给出详细的说明: 1.Merge a range of revisions 此类型应用最为广泛,主要是把源分支中的修改合并到目标分支上来. ...

  9. 函数式接口与Stream流

    lambda表达式是jdk8的特性.lambda表达式的准则是:可推断,可省略. 常规代码写一个多线程 public class Main { public static void main(Stri ...

  10. MongoDB自学------(3)MongoDB文档操作

    一.插入文档 二.查询文档 三.更新文档 可以看到标题(title)由原来的 "Mongodb" 更新为了 "MongoDBtest". 以上语句只会修改第一条 ...