The focus of this article is going to be on utilizing JOINs. We will start off by talking a bit about how JOINs are going to happen and why you need to JOIN data. Then we will take a look at the JOIN types that we have available to us and how to use them.
JOINBASICSJOINs in TSQL are typically going to be done on the FROM line.
Image may be NSFW.
Clik here to view.

Before we are getting to anything else, the real big question becomes ― “Why do we have to do JOINs, and how are we actually going to perform our JOINs?”
As it turns out, every database that we ever work with is going to have its data split up into multiple tables. There are many different reasons for this:
Maintaining data integrity Saving stored space Editing data faster Making queries more flexibleThus, every database that you are going to be working with is going to need that data to be joined together in order for it to actually make sense.
For example, you have separate tables for orders and for customers. The question that becomes ― “How do we actually connect all data together?” That is exactly what JOINs are going to do.
HOW JOINs WORKImagine the case, when we have two separate tables and those tables are going to be brought together by creating a seam.
Image may be NSFW.
Clik here to view.

What is going to happen with the seam, if we get one column from each table that is going to be used for matching, and that is going to determine what rows are or are not going to be returned? For example, we have Customers on the left and ServiceOrders on the right. If we want to get all the customers and their orders, we need to JOIN these two tables together. For this, we need to choose one column that will act as a seam, and obviously, of course, the column we are going to use is CustomerID.
By the way, the CustomerID is known as a Primary Key for the left table, which uniquely identifies every single row inside the Customers table.
In the ServiceOrders table, we also have the CustomerID column, which is known as a Foreign Key . A foreign key is simply a column that is designed to point to another table. In our case, it is pointing back to the Customers table. Therefore, that is how we are going to bring all of that data together by providing that seam.
In these tables, we have the following matchings: 2 orders for 15 and 1 order for 23, 25, and 26. 16 and 22 are left out.
Image may be NSFW.
Clik here to view.

One big thing to note here is that we can JOIN multiple tables . In fact, it is quite common to JOIN multiple tables together, in order to get any form of information. If you take a look at the most common database, you may have to JOIN together four, five, six and more tables just to get the information you are looking for. Having a database diagram is going to be helpful.
Image may be NSFW.
Clik here to view.

To help you out in most database environments you will notice that the columns designed to be JOINed have the same name.
JOINSYNTAXThe third revision of the SQL database query language (SQL-92) regulates the JOIN syntax:
Image may be NSFW.
Clik here to view.

It is possible to do JOINs on WHERE line:
Image may be NSFW.
Clik here to view.

A relation usually hasa simple graphicalinterpretationin the form of a table.
Best Practices and Conventions Alias table names. Use two-part naming for columns Place each JOIN on a separate line Place tables in logical order JOINTYPESSQL Server provides the following types of JOINs:
INNER JOIN OUTER JOIN SELF JOIN CROSS JOIN INNER JOINThe first type of JOINs that we may want to execute is the INNER JOIN. Usually, authors refer to this type of SQL Server JOINs as regular or simple JOIN. They just omit the INNER prefix. This type of JOIN combines two tables together and only returns rows from both sides that match .
Image may be NSFW.
Clik here to view.

We do not see Klara and Anthony here because their CustomerID does not match in both tables. I want to also highlight the fact that the JOIN operation returns a customer each time that it matches the order . There are two orders for Michael and by one order for Dave, Victor, and Tom.
Summary: INNER JOIN returns rows only when there is at least one row in both tables that matches the JOIN condition. INNER JOIN eliminates the rows that do not match with a row from the other table OUTER JOINOuter JOINs are different because they return rows from tables or views even they do not match. This type of JOIN is useful if you need to retrieve all customers who have never placed an order.Or, for example, if you are looking for a product that has never been ordered.
The way that we do our OUTER JOINs is by indicating LEFT or RIGHT, or FULL.
There are no differences between the following clauses:
LEFT OUTER JOIN =LEFT JOIN RIGHT OUTER JOIN =RIGHT JOIN FULL OUTER JOIN =FULL JOINHowever, I would recommend writing the complete clause because it makes the code more readable.
Using LEFT OUTER JOINThere is no difference between LEFT or RIGHT except the fact that we just point the table that we want to get the extra rows from. In the following example, we listed customers and their orders. We utilize the LEFT to get all customers who have never placed orders. We ask SQL Server to get us extra rows from the left table.
Image may be NSFW.
Clik here to view.

Note that Karla and Anthony have not placed any orders and as a result, we get NULL values for the ServiceName and ServiceID. SQL Server doesn’t know what to place in there, and it places NULLs.
Using RIGHT OUTER JOINTo get the less popular service from the ServiceOrders table, we need to use the RIGHT direction.
Image may be NSFW.
Clik here to view.

We see that in this case, SQL Server returned extra rows from the right table, and the Carpet Cleaning service has never been ordered.
Using FULL OUTER JOINThis type of JOIN allows you to get the non-matching information by including non-matching rows from both tables.
Image may be NSFW.
Clik here to view.

This may also be useful if you need to do a data cleanup.
Summary:FULL OUTER JOIN
Returns rows from both tables even if they do not match the JOIN statementLEFT or RIGHT
No difference except in order of tables in FROM clause Direction points at table to retrieve non-matching rows from SELF JOIN The next type of JOINs that we have is SELF JOIN. This is probably the second least common type of JOIN that you are ever going to execute. A SELF JOIN is when you are joining a table onto itself. Generally speaking, this is a sign of poor design. To use the same table twice in a single query, the table must be aliased. The alias helps the query processor identify whether columns should present data from the right or left side. Additionally, you need to eliminate rows marching themselves. Th