查處每門功課前兩名學生的名字,課程名,成績

Table:spa

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

 

SQL:ab

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 DWHERE S.Sno=D.Sno AND C.Cno=D.CnoORDER BY D.Cno ASC

相關文章
相關標籤/搜索