Overview of Types of Joins in SQL Server
The Join clause, the JOIN keyword is merged multiple tables into one result set. The FROM clause is going to have to include all the tables. Separates each table with a comma WHERE clause typically included.
We can put in the Boolean expression that returns true or false, and those expressions can refer to columns in all tables.
Types of Joins in SQL Server
There are different types of Joins
- CROSS JOIN
- INNER JOIN
- OUTER JOIN
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
- FULL OUTER JOIN
- SELF JOIN
Let us understand each of these joins in detail:-
- This is the simplest JOIN
- It returns all rows from both tables
- No WHERE clause
- This is also the least useful
- It’s very inefficient
- It creates a Cartesian Product
- CROSS keyword implied
So everybody from the first table with every piece of data from the second table with no attempt at any sort of matching, so this is the thing you don’t want to do.
Look for FROM clause where you have multiple tables and there is no WHERE clause.
Your initial and last rows have got coordinating values involved with C2. The center rows usually do not meet. The last end result provides all values through the initial as well as, last rows yet do not include that non-matching middle section row.
- This is most typical JOIN
- This emphasized the relational nature of the database
- It allows us to match the column value in one of the tables to the column value in another one of the tables.
- The primary key in one of the tables and a foreign key in one of the other tables.
Here p.person_id is our primary key in our person table,
email_address_person_id is our foreign key in the email address table.
Let’s take a look at the person table
We have got those four rows. And then all the matches inside of the email address table.
So, in the end, should get three people because we have got person Shannon who does not have an email address
So we only get the data where there is a match between the two columns in each of the tables
- The INNER JOIN does not deal with NULL values
- The OUTER JOIN works even when no match in the second table.
- NULL columns if no match in the second table
- FULL OUTER JOIN return all joined rows
- NULL when no match in either table
a)Left Outer Join
Query output: You’re initial as well as, last rows have coordinating values involved with C2. The center rows usually do not meet. The last end result could keep all rows of the first table but actually will leave out the un-matched row through the second table.
- Another NULL-related JOIN
- All rows from the left side will be returned
- NULL for non-matching right side table
You notice that there is one row in the Person table that doesn’t have values in the email address table. So the LEFT OUTER JOIN will give us.
b)Right Outer Join
You’re initial as well as, last rows have coordinating values involved with C2. The center rows usually do not meet. The last end result could keep all rows of the second table but actually will leave out the un-matched row through the first table.
- Opposite of LEFT OUTER JOIN
- All rows from the right side will be returned
- NULL for a non-matching left side table
We get three results. Here’s our RIGHT OUTER JOIN result where we have the row from the email address table that does not have a corresponding row in the right-hand table.
4)Full Outer Join
We get to combine the result of LEFT OUTER JOIN and RIGHT OUTER JOIN
- SELF is not a keyword. It’s a just this idea that you can a table on itself
- Odd but sometimes useful
- No special syntax
- The Same table on left and right side of JOIN
- Useful when a table contains hierarchical data
ON orders.subscription_id = subscriptions.subscription_id;
We get orders table and subscriptions table and JOIN the table using SELECT all columns.
To get the result.
Joins are the way that we really make the relational model come to life. The relational model is about having data separated out, normalized into multiple tables, but able to bring it back together again when we want to see those relationships. And the different kinds of Joins allow us to do that in slightly different ways.
This has been a guide to Types of joins in the SQL server. Here we discuss the overview and types of joins in SQL with their examples. You can also go through our other suggested articles to learn more –