views:

30

answers:

1

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?

+3  A: 
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
Marcelo Cantos
Looks right. Side note - DISTINCT seems unnecessary.
Unreason
ooo thanks a lot