查询表中第二大的数据
要查询第二大的数据,我想着肯定得先把第一大数据查询出来吧:
select max(id) from T ;
然后查询小于最大值的数据:
select * from T where id < (select max(id) from T) order by id desc;
然后取第一条数据:
select * from T where id < (select max(id) from T) and rownum <=1 order by id desc;
好像很正确吧,但是在oracle中,返回的结果却是错误的,正确的做法是:
select * from (
select * from T where id < (select max(id) from T) order by id desc
) where rownum <=1;
先排序,然后再查询结果,嵌套查询。