sql查询第二大的记录(转)

时间:2022-11-09 00:44:11

问题:
  数据库中人表有三个属性,用户(编号,姓名,身高),查询出该身高排名第二的高度。
建表语句

create table users
(
    id int identity(1,1) primary key,
    name varchar(20),
    height float
); 




测试数据

insert into users(name,height) values('zhangsan',182);
insert into users(name,height) values('李四',122);
insert into users(name,height) values('张强',132);
insert into users(name,height) values('王明',122);
insert into users(name,height) values('姜明',182);
insert into users(name,height) values('蒋克龙',182);
insert into users(name,height) values('zhang',182);
insert into users(name,height) values('安安',181);
insert into users(name,height) values('健健',181);
insert into users(name,height) values('明明',181);
insert into users(name,height) values('小小',177);
insert into users(name,height) values('旺旺',123);
insert into users(name,height) values('卡纳',175);
insert into users(name,height) values('文鑫',166);
insert into users(name,height) values('健明',155);
insert into users(name,height) values('拉拉',152);
insert into users(name,height) values('青青',132);
insert into users(name,height) values('乐乐',155);
insert into users(name,height) values('KK',123);
insert into users(name,height) values('FF',122);
insert into users(name,height) values('Lily',152); 

查询语句
1、查询出没有重复值的第二名,即假如最高的身高是182,有几个人同时身高是182,则查出身高小于182的的最高的身高值。
1>、方式1:

select MAX(height) from users where height < (select MAX(height) from users ); 

解析:先查询出最高的身高值,然后查询身高小于该值的最高身高。
2>、方式2:

select top 1 height from users where height not in (select MAX(height) from users) order by height desc; 

解析:先查询出最高的身高,排除该身高,然后将数据进行排序(降序),查询出当前的第一条数据。

3>、方式3:
查询出可能与最高身高值相同的第二名,即假如身高最高是182,有几个人同时是182身高,第二名依然是182.

select top 1 * from  (select top 2 height from users order by height desc) s order by height asc; 

解析:将数据按照身高(降序)排列,查询出前面的两项,然后将这两项按照身高升序,查询第一项目。
http://www.cnblogs.com/0201zcr/p/4820706.html
Oracle版本:

--创建表
declare
  tableNum number;
begin
  select count(1)
    into tableNum
    from user_tables t
   where t.TABLE_NAME = 'USERS';
  dbms_output.put_line('Table Num:' || tableNum);
  if (tableNum = 0) then
    execute immediate 'create table USERS(
  id number(10),
  name varchar2(20),
  height number(3),
  constraint USERS_ID_PK primary key (id))';
  else
    truncate table USERS;
  end if;
  select count(1)
    into tableNum
    from user_tables t
   where t.TABLE_NAME = 'USERS';
  dbms_output.put_line('Table Num:' || tableNum);
  --insert into users (id,name, height) values (1,'zhangsan', 182);
end;

这个块中,在创建表结构后,不能直接insert,会报表不存在
输出结果:
要显示dbms_output.put_line的输入内容,需要在command或sqlplus中执行set serverout on;

Table Num:0
Table Num:1
PL/SQL procedure successfully completed
  insert into users (id,name, height) values (1,'zhangsan', 182);
  insert into users (id,name, height) values (2,'李四', 122);
  insert into users (id,name, height) values (3,'张强', 132);
  insert into users (id,name, height) values (4,'王明', 122);
  insert into users (id,name, height) values (5,'姜明', 182);
  insert into users (id,name, height) values (6,'蒋克龙', 182);
  insert into users (id,name, height) values (7,'zhang', 182);
  insert into users (id,name, height) values (8,'安安', 181);
  insert into users (id,name, height) values (9,'健健', 181);
  insert into users (id,name, height) values (10,'明明', 181);
  insert into users (id,name, height) values (11,'小小', 177);
  insert into users (id,name, height) values (12,'旺旺', 123);
  insert into users (id,name, height) values (13,'卡纳', 175);
  insert into users (id,name, height) values (14,'文鑫', 166);
  insert into users (id,name, height) values (15,'健明', 155);
  insert into users (id,name, height) values (16,'拉拉', 152);
  insert into users (id,name, height) values (17,'青青', 132);
  insert into users (id,name, height) values (18,'乐乐', 155);
  insert into users (id,name, height) values (19,'KK', 123);
  insert into users (id,name, height) values (20,'FF', 122);
  insert into users (id,name, height) values (21,'Lily', 152);
select max(j.height)
  from users j
 where j.height < (select max(s.height) from users s)

sql查询第二大的记录(转)

select g.height
  from (select j.height
          from users j
         where j.height not in (select MAX(j.height) from users j)
         order by j.height desc) g
 where /*rownum < 2*/
 rownum = 1;

sql查询第二大的记录(转)

select g.height
  from (select t.height
          from (select j.height
                  from users j
                 where rownum < 3
                 order by j.height desc) t
         order by t.height asc) g
 where rownum < 2
/* rownum=1*/
;

sql查询第二大的记录(转)