LeetCode //MySQL - 177. Nth Highest Salary

177. Nth Highest Salary

Table: Employee

±------------±-----+
| Column Name | Type |
±------------±-----+
| id | int |
| salary | int |
±------------±-----+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.

Write a solution to find the n t h n^{th} nth highest salary from the Employee table. If there is no n t h n^{th} nth highest salary, return null.

The result format is in the following example.
 

Example 1:

Input:
Employee table:
±—±-------+
| id | salary |
±—±-------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
±—±-------+
n = 2
Output:
±-----------------------+
| getNthHighestSalary(2) |
±-----------------------+
| 200 |
±-----------------------+

Example 2:

Input:
Employee table:
±—±-------+
| id | salary |
±—±-------+
| 1 | 100 |
±—±-------+
n = 2
Output:
±-----------------------+
| getNthHighestSalary(2) |
±-----------------------+
| null |
±-----------------------+

From: LeetCode
Link: 177. Nth Highest Salary


Solution:

Ideas:

1. Declare the necessary variables:

  • nth_highest_salary to store the result.
  • offset to calculate the correct offset for the LIMIT clause.

2. Set the offset variable:

  • SET offset = N - 1; calculates the correct offset for the LIMIT clause.

3. Select the nth highest salary:

  • The subquery selects distinct salaries ordered in descending order.
  • LIMIT offset, 1 uses the offset variable to correctly get the nth highest salary.

4. Return the result.

Code:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  DECLARE nth_highest_salary INT;
  DECLARE offset INT;

  SET offset = N - 1;

  SET nth_highest_salary = (
    SELECT salary
    FROM (
      SELECT DISTINCT salary
      FROM Employee
      ORDER BY salary DESC
      LIMIT offset, 1
    ) AS temp
  );

  RETURN nth_highest_salary;
END;

相关推荐

  1. LeetCode //MySQL - 177. Nth Highest Salary

    2024-06-15 20:32:01       32 阅读
  2. AtCoder ABC172

    2024-06-15 20:32:01       56 阅读
  3. AtCoder ABC175

    2024-06-15 20:32:01       56 阅读
  4. AtCoder ABC176

    2024-06-15 20:32:01       52 阅读
  5. Atcoder ABC179

    2024-06-15 20:32:01       53 阅读
  6. xtu oj 1377 Factorization

    2024-06-15 20:32:01       50 阅读
  7. XDOJ174.分配宝藏

    2024-06-15 20:32:01       52 阅读
  8. 127. 单词接龙

    2024-06-15 20:32:01       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 20:32:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 20:32:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 20:32:01       82 阅读
  4. Python语言-面向对象

    2024-06-15 20:32:01       91 阅读

热门阅读

  1. 【什么是几度cms,主要功能有什么】

    2024-06-15 20:32:01       27 阅读
  2. php中配置variables_order详解

    2024-06-15 20:32:01       33 阅读
  3. React中Hooks--useEffect | useState | useCallback | useMemo

    2024-06-15 20:32:01       21 阅读
  4. 八股系列 Flink

    2024-06-15 20:32:01       22 阅读
  5. 【Qt实现绘制3D图形】

    2024-06-15 20:32:01       22 阅读
  6. 描述React中的函数组件和类组件之间的区别

    2024-06-15 20:32:01       33 阅读
  7. 【设计模式】面向对象的优点

    2024-06-15 20:32:01       32 阅读
  8. React.ReactElement 与 React.ReactNode

    2024-06-15 20:32:01       30 阅读
  9. 程序分享--常见算法/编程面试题:Z 字形变换

    2024-06-15 20:32:01       40 阅读
  10. mysql社区版有可以双机吗

    2024-06-15 20:32:01       24 阅读
  11. 开窗函数MYSQL

    2024-06-15 20:32:01       31 阅读
  12. 使用多进程和 Scrapy 实现高效的 Amazon 爬虫系统

    2024-06-15 20:32:01       29 阅读