查找第二大值的最简单的SQL查询是什么?

时间:2008-08-28 12:39:53

标签: sql puzzle

查找特定列中第二大整数值的最简单的SQL查询是什么?

列中可能存在重复值。

45 个答案:

答案 0 :(得分:268)

SELECT MAX( col )
  FROM table
 WHERE col < ( SELECT MAX( col )
                 FROM table )

答案 1 :(得分:58)

SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);

答案 2 :(得分:30)

在T-Sql中有两种方式:

--filter out the max
select max( col )
from [table]
where col < ( 
    select max( col )
    from [table] )

--sort top two then bottom one
select top 1 col 
from (
    select top 2 col 
    from [table]
    order by col) topTwo
order by col desc 

在Microsoft SQL中,第一种方式的速度是第二种方式的两倍,即使相关列是群集的。

这是因为与max聚合使用的表或索引扫描相比,排序操作相对较慢。

或者,在Microsoft SQL 2005及更高版本中,您可以使用ROW_NUMBER()函数:

select col
from (
    select ROW_NUMBER() over (order by col asc) as 'rowNum', col
    from [table] ) withRowNum 
where rowNum = 2

答案 3 :(得分:17)

我在这里看到了一些特定于SQL Server和一些MySQL特定的解决方案,因此您可能想要澄清所需的数据库。虽然如果我不得不猜测我会说SQL Server,因为这在MySQL中是微不足道的。

我也看到一些不起作用的解决方案,因为它们没有考虑到重复的可能性,所以要小心你接受哪些。最后,我看到一些会起作用但会对表进行两次完整扫描。您希望确保第二次扫描仅查看2个值。

SQL Server(2012年之前):

SELECT MIN([column]) AS [column]
FROM (
    SELECT TOP 2 [column] 
    FROM [Table] 
    GROUP BY [column] 
    ORDER BY [column] DESC
) a

MySQL的:

SELECT `column` 
FROM `table` 
GROUP BY `column` 
ORDER BY `column` DESC 
LIMIT 1,1

<强>更新

SQL Server 2012现在支持更清晰(和standard)OFFSET / FETCH语法:

SELECT TOP 2 [column] 
FROM [Table] 
GROUP BY [column] 
ORDER BY [column] DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;

答案 4 :(得分:12)

我想你可以这样做:

SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT 1 OFFSET 1

SELECT * FROM Table ORDER BY NumericalColumn DESC LIMIT (1, 1)

取决于您的数据库服务器。提示:SQL Server不执行LIMIT。

答案 5 :(得分:7)

您可以使用以下查询找到列的第二大值

SELECT *
FROM TableName a
WHERE
  2 = (SELECT count(DISTINCT(b.ColumnName))
       FROM TableName b WHERE
       a.ColumnName <= b.ColumnName);

您可以在以下链接中找到更多详细信息

http://www.abhishekbpatel.com/2012/12/how-to-get-nth-maximum-and-minimun.html

答案 6 :(得分:6)

最简单的方法是从应用程序中获取此结果集的第二个值:

SELECT DISTINCT value FROM Table ORDER BY value DESC LIMIT 2

但是如果必须使用SQL选择第二个值,那么:

SELECT MIN(value) FROM (SELECT DISTINCT value FROM Table ORDER BY value DESC LIMIT 2) AS t

答案 7 :(得分:4)

查找第二大值的非常简单的查询

SELECT `Column` FROM `Table` ORDER BY `Column` DESC LIMIT 1,1;

答案 8 :(得分:3)

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

此查询将从结果中返回最大工资 - 该结果不包含整个表中的最高工资。

答案 9 :(得分:3)

老问题我知道,但这给了我一个更好的执行计划:

 SELECT TOP 1 LEAD(MAX (column)) OVER (ORDER BY column desc)
 FROM TABLE 
 GROUP BY column

答案 10 :(得分:2)


select * from (select ROW_NUMBER() over (Order by Col_x desc) as Row, Col_1
    from table_1)as table_new tn inner join table_1 t1
    on tn.col_1 = t1.col_1
where row = 2

希望这有助于获得任何行的价值.....

答案 11 :(得分:2)

这是非常简单的代码,你可以试试这个: -

ex: 表名= test

salary 

1000
1500
1450
7500

MSSQL代码获得第二大值

select salary from test order by salary desc offset 1 rows fetch next 1 rows only;

此处'offset 1行'表示第2行表,'仅接收1行'仅用于显示1行。如果你不使用'fetch next next rows',那么它会显示第二行的所有行。

答案 12 :(得分:1)

Tom,相信当select max([COLUMN_NAME]) from [TABLE_NAME]部分中返回的值超过一个时,这将失败。即数据集中有多于2个值的地方。

对您的查询稍作修改即可 -

select max([COLUMN_NAME]) from [TABLE_NAME] where [COLUMN_NAME] **IN** 
  ( select max([COLUMN_NAME]) from [TABLE_NAME] )

答案 13 :(得分:1)

select max(COL_NAME) from TABLE_NAME where COL_NAME in 
    (select COL_NAME from TABLE_NAME where COL_NAME < (select max(COL_NAME) from TABLE_NAME));

子查询返回除最大值之外的所有值。 从返回的列表中选择最大值。

答案 14 :(得分:1)

select min(sal) from emp where sal in 
    (select TOP 2 (sal) from emp order by sal desc)

请注意

sal是col name emp是表名

答案 15 :(得分:1)

<强> MSSQL

SELECT  *
  FROM [Users]
    order by UserId desc OFFSET 1 ROW 
FETCH NEXT 1 ROW ONLY;

<强>的MySQL

SELECT  *
  FROM Users
    order by UserId desc LIMIT 1 OFFSET 1

不需要子查询...只需跳过一行,然后按顺序选择第二行

答案 16 :(得分:1)

select col_name
from (
    select dense_rank() over (order by col_name desc) as 'rank', col_name
    from table_name ) withrank 
where rank = 2

答案 17 :(得分:1)

SELECT 
    * 
FROM 
    table 
WHERE 
    column < (SELECT max(columnq) FROM table) 
ORDER BY 
    column DESC LIMIT 1

答案 18 :(得分:1)

select age from student group by id having age<(select max(age) from student)order by age limit 1

答案 19 :(得分:1)

SELECT MAX(sal) FROM emp
WHERE sal NOT IN (SELECT top 3 sal FROM emp order by sal desc )

这将返回emp表的第三高sal

答案 20 :(得分:1)

正如您提到的重复值。在这种情况下,您可以使用 DISTINCT GROUP BY 来查找第二高的值

这是一张表

  

<强>薪水

enter image description here

  

GROUP BY

SELECT  amount FROM  salary 
GROUP by amount
ORDER BY  amount DESC 
LIMIT 1 , 1
  

DISTINCT

SELECT DISTINCT amount
FROM  salary 
ORDER BY  amount DESC 
LIMIT 1 , 1

LIMIT的第一部分=起始指数

LIMIT的第二部分=多少值

答案 21 :(得分:1)

这是最简单的方式:

SELECT
      Column name
FROM
      Table name 
ORDER BY 
      Column name DESC
LIMIT 1,1

答案 22 :(得分:0)

首先创建一个没有最高工资的虚拟表,然后从虚拟表中查询最大值

SELECT max(salary) from (Select * FROM emp WHERE salary<> (SELECT MAX(salary) from emp)) temp

答案 23 :(得分:0)

select score 
from table 
where score = (select max(score)-1 from table)

答案 24 :(得分:0)

Microsoft SQL Server - 使用两个TOP作为第N个最高值(别名子查询)。

解决第二高的问题:

SELECT TOP 1 q.* 
FROM (SELECT TOP 2 column_name FROM table_name ORDER BY column_name DESC) as q
ORDER BY column_name ASC;

使用TOP两次,但需要别名的子查询。本质上,内部查询按降序排列最大的2个值,然后外部查询按升序翻转,以便第二个最高值现在位于顶部。 SELECT语句返回此顶部。

要求解第n个最高值,请修改子查询TOP值。例如:

SELECT TOP 1 q.* 
FROM (SELECT TOP 5 column_name FROM table_name ORDER BY column_name DESC) as q
ORDER BY column_name;

将返回第5个最高值。

答案 25 :(得分:0)

您可以使用以下查询找到第n个最高值。

    select top 1 UnitPrice from (select distinct top n UnitPrice from 
[Order Details] order by UnitPrice desc) as Result order by UnitPrice asc

这里,n的值为1(最高数字),​​2(第二高数字),​​3(第三高数字)......

答案 26 :(得分:0)

select extension from [dbo].[Employees] order by extension desc offset  2  rows fetch next  1 rows only

答案 27 :(得分:0)

非常简单。 distinct关键字也会处理重复项。

SELECT distinct SupplierID FROM [Products] order by SupplierID desc limit 1 offset 1

答案 28 :(得分:0)

从SQL表中获取倒数第二行的最简单方法是使用ORDER BY ColumnName DESC并设置LIMIT 1,1

试试这个:

SELECT * from `TableName` ORDER BY `ColumnName` DESC LIMIT 1,1

答案 29 :(得分:0)

select max(column_name) from table_name
where column_name not in (select max(column_name) from table_name);

不在中的条件是排除column_name的最高值。

参考:programmer interview

答案 30 :(得分:0)

 SELECT  * FROM `employee` WHERE  employee_salary = (SELECT employee_salary 
 FROM`employee` GROUP BY employee_salary ORDER BY employee_salary DESC LIMIT 
 1,1)

答案 31 :(得分:0)

select MAX(salary) as SecondMax from test where salary !=(select MAX(salary) from test)

答案 32 :(得分:0)

使用此查询。

SELECT MAX ( colname ) 
FROM Tablename 
where colname < (
    SELECT MAX( colname ) 
    FROM Tablename)

答案 33 :(得分:0)

select * from [table] where (column)=(select max(column)from [table] where column < (select max(column)from [table]))

答案 34 :(得分:0)

尝试:

select a.* ,b.* from 
(select * from (select ROW_NUMBER() OVER(ORDER BY fc_amount desc) SrNo1, fc_amount as amount1 From entry group by fc_amount) tbl where tbl.SrNo1 = 2) a
,
(select * from (select ROW_NUMBER() OVER(ORDER BY fc_amount asc) SrNo2, fc_amount as amount2  From entry group by fc_amount) tbl where tbl.SrNo2 =2) b

答案 35 :(得分:0)

这是查找列的第二大值的另一种方法。考虑表'学生'和列'年龄'。然后查询是,

select top 1 Age from Student where Age in(select distinct top 2 Age  from Student order by Age desc) order by Age asc

答案 36 :(得分:0)

SELECT * FROM EMP
WHERE salary=
        (SELECT MAX(salary) FROM EMP
           WHERE salary != (SELECT MAX(salary) FROM EMP)
        );

答案 37 :(得分:0)

我们还可以使用order by和top 1元素,如下所示:

Select  top 1 col_name from table_name
where col_name < (Select top 1 col_name from table_name order by col_name desc)
order by col_name desc 

答案 38 :(得分:0)

查询以查找行中第二高的数字 -

select Top 1 (salary) from XYZ
where Salary not in (select distinct TOP 1(salary) from XYZ order by Salary desc)
ORDER BY Salary DESC

通过将突出显示的Top 1更改为TOP 234,您可以分别找到第3,第4和第5高。

答案 39 :(得分:0)

使用相关查询:

Select * from x x1 where 1 = (select count(*) from x where x1.a < a)

答案 40 :(得分:0)

select * from emp e where 3>=(select count(distinct salary)
    from emp where s.salary<=salary)

此查询选择最多三个工资。如果两个emp获得相同的工资,这不会影响查询。

答案 41 :(得分:0)

请参阅How to select the nth row in a SQL database table?

Sybase SQL Anywhere支持:

SELECT TOP 1 START AT 2 value from table ORDER BY value

答案 42 :(得分:0)

这样的东西?我没有测试过,但是:

select top 1 x
from (
  select top 2 distinct x 
  from y 
  order by x desc
) z
order by x

答案 43 :(得分:0)

这适用于MS SQL:

select max([COLUMN_NAME]) from [TABLE_NAME] where [COLUMN_NAME] < 
 ( select max([COLUMN_NAME]) from [TABLE_NAME] )

答案 44 :(得分:0)

select top 1 MyIntColumn from MyTable
where
 MyIntColumn <> (select top 1 MyIntColumn from MyTable order by MyIntColumn desc)
order by MyIntColumn desc