As we already know, when we implement Normalization, we actually split our main table into sub-tables and remove the redundancy and inconsistency of data. Now, when we make multiple tables and when we need to extract the data from those multiple tables, obviously we need to combine them. This is the place where we need joins. This example is just for the demonstration of the concept where we need joins but it doesn’t mean that we can just implement the joins if we have applied the normalization on the database. The concept is - whenever we need to combine multiple tables, we use joins.
What is a Join?
As we can guess from the name, Join is the SQL statement with the help of which we can combine different tables.
Obviously, joins can only work when there is at least one common attribute in 2 tables. Actually, when we define the relation of 2 tables and define the foreign key in the dependent table which contains the primary key value of the parent table record, this is when both the tables are attached with each other.
Now, we have the relation between 2 tables. And now, we have various ways to combine the tables and extract the data.
- (Inner) Join
- Left (Outer) Join
- Right (Outer) Join
- Full (Outer) Join
Inner Join
It is the most frequently used join. It is also called Equi Join. Inner Join is the type of join which returns the matching values in both tables.
- SELECT InvoiceID
- , Customer.CustomerID
- , FirstName
- , LastName
- , Convert(nvarchar(10), InvoiceDate, 1) AS InvoiceDate
- , Total
- FROM Customer
- Join Invoice
- ON Customer.CustomerId = Invoice.CustomerId
Now, you might have seen the keyword INNER Join; this is also the same thing.
- SELECT InvoiceID
- , Customer.CustomerID
- , FirstName
- , LastName
- , Convert(nvarchar(10), InvoiceDate, 1) AS InvoiceDate
- , Total
- FROM Customer
- INNER Join Invoice
- ON Customer.CustomerId = Invoice.CustomerId
So, we use Inner join to get the matched value records in both tables. Now, let’s suppose we want to join 3 tables.
- SELECT Invoice.InvoiceID
- , Customer.CustomerID
- , FirstName
- , LastName
- , Convert(nvarchar(10), InvoiceDate, 1) AS InvoiceDate
- , Total
- , TrackId
- , UnitPrice
- , Quantity
- FROM ((Customer
- INNER Join Invoice ON Customer.CustomerId = Invoice.CustomerId)
- INNER Join InvoiceLine ON Invoice.InvoiceId = InvoiceLine.InvoiceId)
If you open the database schema, you’ll see we have the attributes with the same names in multiple tables. And if we want to use them in the joins where we’re combining multiple tables, then obviously we need to explicitly specify the complex attribute of which table it belongs to. And here, InvoiceId is also present in InvoiceLine and Invoice tables and both will have the same values whether we use
- InvoiceLine.InvoiceId
- Invoice.InvoiceId
Because it totally depends on check ON condition.
Correlation Table Names
It allows you to assign a temporary and shorter name of the table. Sometimes, they are called table alias.
- SELECT IL.InvoiceID
- , C.CustomerID
- , FirstName
- , LastName
- , Convert(nvarchar(10), InvoiceDate, 1) AS InvoiceDate
- , Total
- , TrackId
- , UnitPrice
- , Quantity
- FROM ((Customer AS C
- INNER Join Invoice AS I ON C.CustomerId = I.CustomerId)
- INNER Join InvoiceLine AS IL ON I.InvoiceId = IL.InvoiceId)
Look, we used AS keyword for the table alias.
Fully Qualified Object Names
In Fully Qualified Object Names, we specify the complete names.
Server.Database.Schema.Object
Actually when we’re combining the tables of multiple servers then we use this approach. It makes the better understandability and makes everything clear both to SQL engine and human as well.
- SELECT CustomerID
- , FirstName
- , LastName
- FROM [DESKTOP-2FA7R8U].Chinook.dbo.Customer
As you can see we are using the brackets around the server name because we are using the dash into the server name. The word dbo stands for Database Owner.
Left Outer Join
You might be confused about Left join by watching this diagram.
You might expect that we’ll get all the columns of table 1 and matching columns of table 2 with table 1. But it is not the case actually. With the help of left join, we’ll get all the records (rows) from the left table and all the matching records from table 2.
So don’t expect when you left join 2 tables, you’ll get all the columns from first table.
- SELECT c1.FirstName + ' ' + c1.LastName AS CustomerName
- , c2.FirstName + ' ' + c2.LastName AS EmployeeName
- FROM Customer c1
- LEFT JOIN Employee c2
- ON c1.SupportRepId = c2.EmployeeId
Right Join
It returns all the records from the right table and all the matching records from left table.
And the query is very similar to Left Join.
- SELECT c1.FirstName + ' ' + c1.LastName AS CustomerName
- , c2.FirstName + ' ' + c2.LastName AS EmployeeName
- FROM Customer c1
- Right JOIN Employee c2
- ON c1.SupportRepId = c2.EmployeeId

Join the conversation! Your thoughts help the community grow.