zoukankan      html  css  js  c++  java
  • [SQL]LeetCode177. 第N高的薪水 | Nth Highest Salary

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10145510.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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

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

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

    140ms
    1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    2 BEGIN
    3     set N = N-1;
    4   RETURN (
    5       # Write your MySQL query statement below.
    6       select distinct Salary from Employee order by Salary desc limit N,1
    7   );
    8 END

    149ms

     1 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
     2 BEGIN
     3   set N = N-1;
     4   RETURN (      
     5       select distinct Salary as 'getNthHighestSalary(2)'
     6       from Employee
     7       order by Salary desc
     8       limit N, 1
     9   );
    10 END
  • 相关阅读:
    OpenCV中threshold函数的使用
    opencv中namedWindow( )函数
    Open CV leaning
    RGB颜色表
    visual studio 2015 Opencv4.0.1配置
    uint16_t
    Arduino重置-复位问题
    bzoj1823 [JSOI2010]满汉全席(2-SAT)
    bzoj2208 [Jsoi2010]连通数(scc+bitset)
    UVAlive3713 Astronauts(2-SAT)
  • 原文地址:https://www.cnblogs.com/strengthen/p/10145510.html
Copyright © 2011-2022 走看看