LeetCode 176. Second Highest Salary
Posted 浩然
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 176. Second Highest Salary相关的知识,希望对你有一定的参考价值。
https://leetcode.com/problems/second-highest-salary/description/
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 query should return 200
as the second highest salary. If there is no second highest salary, then the query should return null
.
+---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+
Solution
Approach: Using sub-query and LIMIT
clause [Accepted]
Algorithm
Sort the distinct salary in descend order and then utilize the LIMIT
clause to get the second highest salary.
SELECT DISTINCT
Salary AS SecondHighestSalary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
However, this solution will be judged as ‘Wrong Answer‘ if there is no such second highest salary since there might be only one record in this table. To overcome this issue, we can take this as a temp table.
SELECT
(SELECT DISTINCT
Salary
FROM
Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1) AS SecondHighestSalary
;
The LIMIT
clause can be used to constrain the number of rows returned by the SELECT
statement. LIMIT
takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:
-
Within prepared statements,
LIMIT
parameters can be specified using?
placeholder markers. -
Within stored programs,
LIMIT
parameters can be specified using integer-valued routine parameters or local variables.
With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT
is equivalent to row_count
LIMIT 0,
.row_count
For prepared statements, you can use placeholders. The following statements will return one row from the tbl
table:
SET @a=1;
PREPARE STMT FROM ‘SELECT * FROM tbl LIMIT ?‘;
EXECUTE STMT USING @a;
The following statements will return the second to sixth row from the tbl
table:
SET @skip=1; SET @numrows=5;
PREPARE STMT FROM ‘SELECT * FROM tbl LIMIT ?, ?‘;
EXECUTE STMT USING @skip, @numrows;
For compatibility with PostgreSQL, MySQL also supports the LIMIT
syntax.row_count
OFFSET offset
If LIMIT
occurs within a subquery and also is applied in the outer query, the outermost LIMIT
takes precedence. For example, the following statement produces two rows, not one:
(SELECT ... LIMIT 1) LIMIT 2;
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 # Write your MySQL query statement below 8 SELECT 9 (SELECT DISTINCT Salary 10 FROM Employee 11 ORDER BY Salary DESC 12 LIMIT 1 OFFSET 1) AS SecondHighestSalary;
以上是关于LeetCode 176. Second Highest Salary的主要内容,如果未能解决你的问题,请参考以下文章
[LeetCode] 176. Second Highest Salary_Easy tag: SQL