找第二大的数SQL-Second Highest Salary
Posted 哈哈哈
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找第二大的数SQL-Second Highest Salary相关的知识,希望对你有一定的参考价值。
1: 找小于最大的最大的
select max(Salary) from Employee where Salary<(select MAX(Salary) from Employee);
2. 排序
select Salary from Employee where Salary not in (select MAX(Salary)from Employee) order by Salary desc limit 1;
select ( select distinct Salary from Employee order by Salary Desc limit 1 offset 1 ) as SecondHeighestSalary;
找第n个数:
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN DECLARE M INT; set N=N-1; RETURN ( # Write your mysql query statement below. select Salary from Employee order by Salary desc limit 1 offset N ); END
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN DECLARE M INT; set N=N-1; RETURN ( # Write your MySQL query statement below. select distinct Salary from Employee order by Salary desc limit 1 offset N ); END
不能在limit 里N-1, 因为limit里不计算
哈哈: distinct :在表中可能包含重复值,返回唯一不同的值,
以上是关于找第二大的数SQL-Second Highest Salary的主要内容,如果未能解决你的问题,请参考以下文章