美文网首页
leetcode--177--第N高的薪水

leetcode--177--第N高的薪水

作者: minningl | 来源:发表于2020-03-28 15:57 被阅读0次

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

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

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

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

    链接:https://leetcode-cn.com/problems/nth-highest-salary

    思路:
    1、这题和176题类似,主要考察sql中limit函数的用法。此外还考察了sql中函数的用法,变量赋值的用法

    SQL如下:

    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
      SET N = N - 1;
      RETURN (
          # Write your MySQL query statement below.
          select (
            select distinct Salary
              from Employee
             order by Salary desc
             limit N,1
           ) as SecondHighestSalary
      );
    END
    

    相关文章

      网友评论

          本文标题:leetcode--177--第N高的薪水

          本文链接:https://www.haomeiwen.com/subject/neqsuhtx.html