zoukankan      html  css  js  c++  java
  • [LeetCode]-DataBase-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.

    需求:查询第N高的工资

    CREATE TABLE Employee(
    Id TINYINT UNSIGNED,
    Salary DECIMAL(10,2)
    )ENGINE=MyISAM CHARSET=utf8;


    -- sql 使用 limit 和 ORDER BY
    DROP FUNCTION IF EXISTS getNthHighestSalary;
    CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
    BEGIN
    DECLARE m INT;
    SET m = n -1;
    RETURN (
    # Write your MySQL query statement below.
    SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT m,1
    );
    END

  • 相关阅读:
    存储过程
    .Net经典面试题
    《锋利的Jquery》
    WPF-1
    ios-5-类别和协议
    ios-4-创建单例模式
    ios-3-简单内存管理
    ios-2
    ios -1
    <<ASP.NET MVC4 Web编程>>笔记
  • 原文地址:https://www.cnblogs.com/lianliang/p/5303069.html
Copyright © 2011-2022 走看看