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?
The syntax for multiple joins: SELECT column_name1,column_name2,.. FROM table_name1 INNER JOIN table_name2 ON condition_1 INNER JOIN table_name3 ON condition_2 INNER JOIN table_name4 ON condition_3 . . . Note: While selecting only particular columns use table_name.
As you can see, joining three tables in SQL isn't as hard as it sounds. In fact, you can join as many tables as you like – the idea behind it is the same as joining only two tables. It's very helpful to take a look at the data midstep and imagine that the tables you've already joined are one table.
In SQL Server, you can join more than two tables in either of two ways: by using a nested JOIN , or by using a WHERE clause. Joins are always done pair-wise.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With