zoukankan      html  css  js  c++  java
  • LeetCode 177. Nth Highest Salary

    https://leetcode.com/problems/nth-highest-salary/description/

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

     1 Create table If Not Exists Employee (Id int, Salary int);
     2 Truncate table Employee;
     3 insert into Employee (Id, Salary) values ('1', '100');
     4 insert into Employee (Id, Salary) values ('2', '200');
     5 insert into Employee (Id, Salary) values ('3', '300');
     6 
     7 CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
     8 BEGIN
     9 DECLARE M INT;
    10 SET M = N - 1;
    11   RETURN (
    12       # Write your MySQL query statement below.
    13       SELECT
    14         (SELECT Salary
    15         FROM Employee
    16         ORDER BY Salary DESC
    17         LIMIT M, 1) AS getNthHighestSalary      
    18   );
    19 END
    View Code
     
  • 相关阅读:
    TCP 基础知识
    Spring Boot 实战 —— 日志框架 Log4j2 SLF4J 的学习
    MySQL 实战笔记
    Java 基础
    RPM 包的构建
    RPM 包的构建
    9. 桶排序
    8. 基数排序
    7. 计数排序
    6. 快速排序
  • 原文地址:https://www.cnblogs.com/pegasus923/p/7654583.html
Copyright © 2011-2022 走看看