日期:2014-05-19  浏览次数:20501 次

请教一SQL语句
做练习题时遇到的一问题:
有张考试成绩表ExamTab  

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

------解决方案--------------------
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 ' , 45

select 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差