Join three tables in SQL.
In this video, you'll learn how to join three tables in SQL.
Writing queries that involve three or more tables in SQL is something you'll do quite
often as you get more experienced with SQL.
Doing it for the first time can seem hard, but once you understand how to do it, it's
a lot easier.
Let's take a look at how it's done.
In SQL you join tables together to get data from each [inaudible] in your query.
You need to join three tables together because the data you need is in three different tables.
So how do you do it?
The first step is to select your columns from the first table.
This involves writing a select query with your columns and your table.
In this example, we're selecting some information from a book table.
Step two is the join the second table, we do this by specifying the type of join after
the from clause and then the table name.
You can do either an inner join, a left join or a right join.
There are a couple of other join types, but in most cases, your join will be one of these
three types.
So enter in your join type and then your table name.
In this example, we will join to the publisher table.
Now we've joined to the publisher table, we need to specify the columns.
So entering the columns that are to be joined on, these will be the columns in each table
that store the same value.
This is often the foreign key in one table and the primary key in the other.
For example, we want to join book and publisher.
We specify on, then book.publisher ID equals publisher.publisher ID.
This means we're joining the two tables on these two columns, the publisher ID.
Step four is to add the columns from this table to the select clause.
If there are any columns you want to show and there might not be.
But if there are, then add them to the select clause, so they are included in your results.
In this example, we're adding the publisher name from the publisher table.
Now we can join to the third table, to do this we add another join clause after we've
joined the second table.
So on a new line, after your existing join clause, repeat steps two and three.
Enter the join type, such as inner join.
It can be a different join type to other joints.
Then enter the table you want to join to.
In this example, we are going to join to the book language table to get the language of
the book.
Now we've added the title, we add the columns we want to join on.
We specify the column from this table, as well as the column from either of the tables
we've already added.
We can join to either the book table or the publisher table, it doesn't matter.
It depends on how our tables are linked.
In this example, we want to join to the book table as that's where the language ID is located.
So our query would look like this, in a join book language on book.language ID equals book
language.language ID.
Finally, we add whatever columns we want to see in our results from this third table into
our select clause.
In this example, we add the language column.
Now our query is done, we have successfully joined three tables together and displayed
columns from the three tables.
We can run this query and it will give us the data from three tables.
If you learned something new from this video, make sure to subscribe to my channel.
If you want to learn more about database design and development, visit databasestar.com.
That's where I share my best database related content.
Which step from this tutorial has been the most helpful for you?
Was it step five, as we just needed to repeat the earlier steps for the new table?
Or step four, where we add the columns to the select clause?
Or something else?
Thanks for watching.