select *
from students T
where id in (select top 3 id from students where t.class = class)

解决方案 »

  1.   

    select * from 表 aa where (select count(*) from 表 bb where aa.class=bb.class and bb.id<aa.id) <=3
      

  2.   

    try:
    select class,b.s_name from yourTable a 
    join (Select top 3 class,s_name from yourTable where class = a.class) b
    on a.class = b.class
      

  3.   

    select * from student a 
    where id in(select top 3 id from student where class=a.class)
      

  4.   

    select * from student a 
    where id in(select top 3 id from student where class=a.class)
    order by class
      

  5.   

    select *
    from students a
    where id in (select top 3 id from students where a.class = class)
      

  6.   

    数据测试--创建数据测试环境
    declare @students table(id int,class int,s_name varchar(10))
    insert into @students
    select 9,27,'ss'
    union all select 10,27,'aa'
    union all select 11,27,'bb'
    union all select 12,27,'cc'
    union all select 13,28,'dd'
    union all select 14,28,'ee'
    union all select 15,28,'dd'
    union all select 16,28,'ee'
    union all select 17,29,'dd'
    union all select 18,29,'ee'
    union all select 19,29,'gg'
    union all select 20,29,'aa'--显示查询结果
    select * from @students a 
    where id in(select top 3 id from @students where class=a.class)
    /*--执行结果为:id          class       s_name     
    ----------- ----------- ---------- 
    9           27          ss
    10          27          aa
    11          27          bb
    13          28          dd
    14          28          ee
    15          28          dd
    17          29          dd
    18          29          ee
    19          29          gg(所影响的行数为 9 行)-------------------------------------*/
      

  7.   

    select *
    from students T
    where id in (select top 3 id 
                   from students 
                  where t.class = class
               order by newid()
                )
      

  8.   

    select *
    from students a
    where id in (select top 3 id from students where a.class = class)
    order by a.class,a.s_name
      

  9.   

    select * from student where 
    ( select count(*) from  student s
      where  student.class=s.class and student.id>=s.id) <=3
     order by  class,id
      

  10.   

    摘自
    一道褒贬不一的 SQL 考试题    playyuer(原作)
    http://www.csdn.net/Develop/Read_Article.asp?Id=15989
    10.列印各科成绩前三名的记录:(不考虑成绩并列情况)
      学生ID,学生姓名,课程ID,课程名称,成绩,教师ID,教师姓名  如果仅从成绩考虑前三名的人,利用相关子查询的知识: SELECT * 
       FROM 成绩表 t1
      WHERE 成绩 IN (SELECT TOP 3 成绩
                   FROM 成绩表
                   WHERE t1.课程id = 课程id
                ORDER BY 成绩 DESC
                  )
    ORDER BY t1.课程id  这样查询的结果各科成绩前三名的记录数应该大于等于三,因为可能有并列情况,
      如果小于三自然是该门课还没有那么多人考试!
      如果不考虑并列情况,严格控制各科只列印三条记录,则使用"学生id"构造相关
      子查询条件亦可:  SELECT * 
        FROM 成绩表 t1
       WHERE 学生id IN (SELECT TOP 2 学生id
                          FROM 成绩表
                         WHERE t1.课程id = 课程id
                      ORDER BY 成绩 DESC
                        )
    ORDER BY t1.课程id  如果利用第 10 题的思路也可实现该应用。  最后还要多说一句: 一般 TOP 关键字与 ORDER BY 子句合用才有真正意义。
      

  11.   

    select * from students where (select count(*) from students a  where students.class=a.class and students.id<>s.id)<=3
    order by id,class