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
精彩评论