user450143
user450143

Reputation: 435

SQL Server 2008 - How to Join 3 tables

SQL Server 2008:

I have 3 tables

Users, Scores, Lessons

Users & Scores are linked by StudentID

Scores & Lessons are linked by LessonID

I want to display the scores for a StudentID. Here are the columns I want to display

Users.Name, Scores.LessonID, Scores.Result, Lessons.Title

I know how to Join the 2 tables. How do I throw in the 3rd table?

Upvotes: 16

Views: 65181

Answers (2)

Lokanathan
Lokanathan

Reputation: 201

 SELECT *
 FROM   T1
   INNER JOIN T2
     ON T2.C = T1.C
   INNER JOIN T3
              LEFT JOIN T4
                ON T4.C = T3.C
     ON T3.C = T2.C 


is equivalent to (T1 Inner Join T2)  Inner Join (T3 Left Join T4)

Upvotes: 0

Alex J
Alex J

Reputation: 10205

Same way as one table:

SELECT Users.Name, Scores.LessonID, Scores.Result, Lessons.Title
FROM Users
INNER JOIN Scores ON Users.StudentID = Scores.StudentID
INNER JOIN Lessons On Scores.LessonID = Lessons.LessonID

Upvotes: 28

Related Questions