开发者

SQL database queries basic question AVG Difference

I have a really basic question on SQL.

Relational Schema:

  • Student (Sid,开发者_运维技巧 Sname )
  • Class (Sid, ClassName, mark)
  • ClassName = { MATH, ENGLISH, PHY, .......}

I want to list out all the student who have taken MATH, and their MATH class average minus the student's math mark

Select DINSTINCT S.Sname, (E.Mark - AVG (E.Mark))   
From Student As S, Class As C   
Where C.ClassName = 'MATH'  AND S.Sid = C.Sid

I don't know how to do difference; I don't think this is right; can someone tell me how to fix this?


Select DISTINCT S.Sname, E.Mark - (SELECT AVG(E.Mark)
                                     FROM Student
                                    WHERE C.ClassName = 'MATH')
From Student As S, Class As C   
Where C.ClassName = 'MATH'  AND S.Sid = C.Sid
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜