做练习题时遇到的一问题:
有张考试成绩表ExamTab 

    id int not null,
    sex char(4) not null
    name char(20) not null
    score int not null
 )
请问:如何查询出成绩倒4差的考生的姓名以及成绩。

解决方案 »

  1.   

    select top 4 * from tablename order by score
      

  2.   

    caixia615(*^_^*),不好意思,是我意思没表达清楚,最后查询的结果只有一条,而且是很具体的考生的姓名以及成绩
    比如:result:
    name     score
    ----------
    ccc      60
      

  3.   

    select top 1 * from tablename order by score desc
      

  4.   

    还是不正确:如: 
    id    sex  name  score 
    1001   F   aa     100 
    1002   F   bb     70 
    1003   M   cc     80 
    1004   F   dd     90 
    1005   M   ee     60
    1006   M   ff     50
    1007   M   gg     65
    1008   F   hh     95
    1009   M   gg     85
    1010   F   hh     75
    1011   M   gg     45
    最终结果为(成绩排名倒数第5差的考生):
    name   score
    -----------------
    bb     70 
      

  5.   

    declare @t table(id int,    sex varchar(20), name varchar(20), score int)
    insert @t select
    1001,   'F',   'aa' ,    100 
    union all select
    1002,   'F' ,  'bb' ,    70 
    union all select
    1003,   'M',   'cc' ,    80 
    union all select
    1004,   'F',   'dd' ,    90 
    union all select
    1005,   'M',   'ee' ,    60
    union all select
    1006,   'M',   'ff' ,    50
    union all select
    1007,   'M',   'gg' ,    65
    union all select
    1008,   'F',   'hh'  ,   95
    union all select
    1009,   'M',   'gg'  ,   85
    union all select
    1010,   'F' ,  'hh'  ,   75
    union all select
    1011,   'M',   'gg'  ,   45select top 1 * from (select * from @t where id not in (select top 4 id from @t order by score))a order by score
    id          sex                  name                 score       
    ----------- -------------------- -------------------- ----------- 
    1002        F                    bb                   70(所影响的行数为 1 行)第5差
      

  6.   

    louifox(兰陵笑笑生) ok ,right !! 谢谢各位的帮助,我明白了,思路也清晰了!!!