SQL implementation LeetCode (176. Second highest salary)

SQL implementation LeetCode (176. Second highest salary)

[LeetCode] 176. Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

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

For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.

This question asks us to find the second largest number in a column of a table. There are many ways to solve this problem. Let's first look at a solution using the two keywords Limit and Offset. The number after Limit in MySQL limits the number of data we return, and Offset is the offset. So if we want to find the second highest salary, we can first sort the salaries in descending order, and then we set Offset to 1, which means starting from the second one, that is, the second highest salary, and then we set Limit to 1, which means only taking out the second highest salary. If Limit is set to 2, then both the second and third highest salaries will be taken out:

Solution 1:

SELECT Salary FROM Employee GROUP BY Salary
UNION ALL (SELECT NULL AS Salary)
ORDER BY Salary DESC LIMIT 1 OFFSET 1;

We can also use the Max function, which returns the maximum value. The logic is that we take out the maximum value of the numbers that do not contain the maximum value, which is the second largest value:

Solution 2:

SELECT MAX(Salary) FROM Employee 
WHERE Salary NOT IN
(SELECT MAX(Salary) FROM Employee);

The following method is basically the same as above, except that the less than sign < is used instead of the Not in keyword, and the effect is the same:

Solution 3:

SELECT MAX(Salary) FROM Employee
Where Salary <
(SELECT MAX(Salary) FROM Employee);

Finally, let's look at a method that can be extended to find the Nth highest salary. Just change the 1 in the following statement to N-1. The second highest salary is 1 when N-1 is substituted. The logic of the following statement is that if we want to find the second highest salary, then we allow one of the maximum values ​​to exist, and then find the largest value among the remaining numbers, which is the second largest value of the whole.

Solution 4:

SELECT MAX(Salary) FROM Employee E1
WHERE 1 =
(SELECT COUNT(DISTINCT(E2.Salary)) FROM Employee E2
WHERE E2.Salary > E1.Salary);

References:

https://leetcode.com/discuss/47041/very-very-simple-solution

https://leetcode.com/discuss/42849/general-solution-not-using-max

https://leetcode.com/discuss/21751/simple-query-which-handles-the-null-situation

This is the end of this article about SQL implementation of LeetCode (176. The second highest salary). For more relevant SQL implementation of the second 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 of LeetCode (177. Nth highest salary)
  • SQL implementation LeetCode (185. Top three highest salaries in the department)

<<:  Use h1, h2, and h3 tags appropriately

>>:  Implementation idea of ​​left alignment of the last row of flex box layout

Recommend

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

Detailed steps to install MYSQL8.0 on CentOS7.6

1. Generally, mariadb is installed by default in ...

Discussion on the numerical limit of the ol element in the html document

Generally speaking, it is unlikely that you will ...

js realizes the dynamic loading of data by waterfall flow bottoming out

This article shares with you the specific code of...

Detailed explanation of basic operation commands for Linux network settings

Table of contents View network configuration View...

Html+CSS drawing triangle icon

Let’s take a look at the renderings first: XML/HT...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

Method of dynamically loading geojson based on Vue+Openlayer

Load one or more features <template> <di...

MySQL database SELECT query expression analysis

A large part of data management is searching, and...

5 MySQL GUI tools recommended to help you with database management

There are many database management tools for MySQ...

An example of the difference between the id and name attributes in input

I have been making websites for a long time, but I...

Specific method to delete mysql service

MySQL prompts the following error I went to "...

How to migrate sqlite to mysql script

Without further ado, I will post the code for you...

CSS to achieve single-select folding menu function

Don’t introduce a front-end UI framework unless i...