Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 772
user450143 Avatar asked May 07 '11 10:05

user450143


People also ask

How join 3 tables inner join SQL?

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.

Can we merge 3 tables in SQL?

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.

Can you join more than 2 tables in SQL?

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.


1 Answers

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
like image 148
Alex J Avatar answered Nov 13 '22 17:11

Alex J