SQL implementation of LeetCode (177. Nth highest salary)

SQL implementation of LeetCode (177. Nth highest salary)

[LeetCode] 177.Nth Highest Salary

Write a SQL query to get the n th 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.

This question is an extension of the previous Second Highest Salary question. Based on the approach of the previous question, we can easily extend it to N. According to the analysis of Solution 1 in Second Highest Salary, we only need to change the 1 after OFFSET to N-1. However, MySQL will report an error, probably because it does not support the operation. Then we can add a SET N = N - 1 in front to change N to N-1 first and then do the same:

Solution 1:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  SET N = N - 1;
  RETURN (
      SELECT DISTINCT Salary FROM Employee GROUP BY Salary
      ORDER BY Salary DESC LIMIT 1 OFFSET N
  );
END

According to the analysis of Solution 4 in Second Highest Salary, we only need to change 1 to N-1. However, here we support the calculation of N-1. See the code below:

Solution 2:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      SELECT MAX(Salary) FROM Employee E1
      WHERE N - 1 =
      (SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2
      WHERE E2.Salary > E1.Salary)
  );
END

Of course, we can also change the last > to >=, so that we can replace N-1 with N:

Solution 3:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
  RETURN (
      SELECT MAX(Salary) FROM Employee E1
      WHERE N =
      (SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2
      WHERE E2.Salary >= E1.Salary)
  );
END

Similar topics:

Second Highest Salary

References:

https://leetcode.com/discuss/88875/simple-answer-with-limit-and-offset

https://leetcode.com/discuss/63183/fastest-solution-without-using-order-declaring-variables

This is the end of this article about SQL implementation of LeetCode (177. The Nth highest salary). For more relevant SQL implementation of the Nth highest salary content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • SQL implementation of LeetCode (184. The highest salary in the department)
  • SQL implementation of LeetCode (183. Customers who have never placed an order)
  • SQL implementation of LeetCode (182. Duplicate mailboxes)
  • SQL implementation of LeetCode (181. Employees earn more than managers)
  • SQL implements LeetCode (180. Continuous numbers)
  • SQL implementation of LeetCode (178. Score ranking)
  • SQL implementation LeetCode (176. Second highest salary)
  • SQL implementation LeetCode (185. Top three highest salaries in the department)

<<:  The English reading of various special symbols on the keyboard (knowledge popularization)

>>:  Docker container explains in detail how to simplify the image and reduce the size

Recommend

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

Example of using setInterval function in React

This article is based on the Windows 10 system en...

Detailed explanation of reduce fold unfold usage in JS

Table of contents fold (reduce) Using for...of Us...

Win10 + Ubuntu20.04 LTS dual system boot interface beautification

Effect display The built-in boot interface is too...

An article teaches you how to implement a recipe system with React

Table of contents 1. Recipe Collection 1.1 Projec...

Apache Calcite code for dialect conversion

definition Calcite can unify Sql by parsing Sql i...

How to split and merge multiple values ​​in a single field in MySQL

Multiple values ​​combined display Now we have th...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

TortoiseSvn Little Turtle Installation Latest Detailed Graphics Tutorial

There were always problems when installing tortoi...

Detailed explanation of cross-usage of Ref in React

Table of contents 1. First, let’s explain what Re...

How to use async and await in JS

Table of contents 1. async 2. await: 3. Comprehen...