Monday, February 27, 2012

Help with nested Query ?

I have three Tables Student, Courses, Marks

Table : Student
Columns

StudentID <PK>
First Name
Last Name

Table : Grades
Columns
StudentID <FK>
Grade

Table : Courses
Columns
StudentID <FK>
CourseID
CourseDesc

Now to get all the course descriptions which this particular student is taking based on the StudentID we do something like this :

SELECT c.courseDesc
FROM Courses c, Student s
WHERE s.StudentID = '100'
AND s.StudentID = c.StudentID

The above will work

But If I need to do it in nested query how can I do it : Something like

SELECT * FROM

(

SELECT c.courseDesc
FROM Courses c, Student s
AND s.StudentID = c.StudentID

)

WHERE s.StudentID = '100'

Thanks for the help.


Harsimrat

If I do something like this, it should work and its not happy

SELECT * FROM

(

SELECT c.courseDesc, s.StudentID

FROM Courses c, Student s

WHERE s.StudentID = c.StudentID

)

WHERE s.StudentID = '100'

|||

You need to give the derived table an alias to get it to work

Code Snippet

SELECT * FROM

(

SELECT c.courseDesc, s.StudentID

FROM Courses c, Student s

WHERE s.StudentID = c.StudentID

) as items

WHERE s.StudentID = '100'

Though, why the subquery?

No comments:

Post a Comment