sql返回表中薪资第2/第N高数据
176. Second Highest Salary
Easy
要求:返回表中薪资第二高数据
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 | +---------------------+
select (
select distinct Salary
from
Employee
order by Salary desc limit 1,1) as 'SecondHighestSalary'
177.
要求:返回表中薪资第N高数据 (与上方方法相似,但是需要注意索引是从0开始,所以要将set N = (N-1);
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N = (N-1);
RETURN (
# Write your MySQL query statement below.
select (select distinct Salary from Employee order by Salary desc limit N,1 )as 'getNthHighestSalary'
);
END
浙公网安备 33010602011771号