查处每门功课前两名学生的名字,课程名,成绩

Table:

Student(Sno,Sname,Sage,Ssex)
Course(Cno,Cname,Teacher)
SC(Sno,Cno,Grade)

 

SQL:

SELECT S.Sname ,C.Cname,D.Grade
FROM Student S,Course C,
(  

  --这部分是关键:

  SELECT *

  FROM dbo.SC SCA

  WHERE grade IN
  (
    SELECT DISTINCT TOP 2 grade

    FROM SC
    WHERE SCA.Cno=Cno
    ORDER BY Grade DESC
  )

) AS D
WHERE S.Sno=D.Sno AND C.Cno=D.Cno
ORDER BY D.Cno ASC

 

posted @ 2015-09-16 18:05  zp_Alex  阅读(429)  评论(0编辑  收藏  举报