24
Learn SQL: Microsoft SQL Server - Episode 5: Joining Tables
![](https://res.cloudinary.com/practicaldev/image/fetch/s--2Jf4ou6x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rqs1t876uy30hbty6nbt.png)
Thus far we have discussed how to pull data from a single table. However in most cases, we will need to pull the data from multiple tables in order to get the bigger picture for our analysis or reporting.
Before we can join tables in a SQL query, we need to be familiar with our database structure, which tables there are, what kind of columns are there, and how the tables connect to each other.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif)
In the AdventureWorks, the Person schema contains a-lot of information about a person, the people who are in the database, the person table contains columns that hold different pieces of information of a person.
It holds the Primary Key (PK), which is BusinessEntityID. This means that BusinessEntityID is a unique record that identifies each record.
BusinessEntityID is also the Foreign Key (FK) for this table. This means it is used to connect to other related tables.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--niXrbAnF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju6060muhkka8iya3j9g.gif)
There is also a table called Person.EmailAddress, which contains information on each persons email address.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif)
The table has a the Primary Key of BusinessEntityID and each email address has a Primary Key of EmailAddressID, the EmailAddressID allows for a person to have multiple email addresses.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--WvS_ejQF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq9yo667pdjfam6e29pq.gif)
In order for us to pull the firstName, lastName and emailAddress for a person, we need to join these two tables. A way to join them is to take a key that is common between both of them, which in this case is BusinessEntityID.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--0xJBUtqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuhccgyk7ddmkgjcsib9.gif)
The syntax for joining tables is as follows:
Select [Column Names | * ]
From [Table Name]
On [Table.primaryKey] = [Table.primaryKey]
![](https://res.cloudinary.com/practicaldev/image/fetch/s--D5Tanx67--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gafhtkwz7iiq1chdjbed.gif)
We can limit the amount of columns return to us by specifying the columns we want to pull in the select statement.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--doYalvrx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dku8l2im8y4rrgxbkin3.gif)
Notice how when we try to pull the data from BusinessEntityID, SQL throws an error, it will also indicate an error by underlining with a red line. The reason why is we an not being specific from which table we want to pull the BusinessEntityID, since it exist in both tables.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--FJKPXAKM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0pocki1vzwvoyfb1g4wj.gif)
When we specify we want to pull from the person.BusinessEntityID is works perfectly.
Using this same syntax we can join multiple tables. Lets say for instance we need to join another table and it is the Person.PersonPhone table.
![](https://res.cloudinary.com/practicaldev/image/fetch/s--4g6R117C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxop7go1vcneu08kc5yn.gif)
We can even limit this further by specifying a Where clause, lets say for instance we need to limit this query to where the firstName is Kim, we can accomplish this as follows:
![](https://res.cloudinary.com/practicaldev/image/fetch/s--y3WR_cCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlzfd5zxuseps5cjmfts.gif)
We are now able to find each person with the first name as Kim, we can see their last name, email address and phone number by joining multiple tables, that is pretty amazing!
![](https://res.cloudinary.com/practicaldev/image/fetch/s--whlqc0KN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avgigriowxz6i6241uvl.png)
We have come to the end of this discussion about joining tables, I really hope you enjoyed reading it, join me in the next episode...
![](https://res.cloudinary.com/practicaldev/image/fetch/s--kNN4pcPb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkpp1xl6761axpfzu7y0.gif)
24