Overview of Types of Joins in SQL Server
Joins are the types of a single concept, which allows the joining of two or more tables using a defined syntax in SQL programming. Joining of the tables being facilitated through a common field which is present in each of the tables, either by same or different names, and the joins being characterized into various types, based on the number and the nature of records extracted from the tables by the SQL query, such as inner join, left outer join, right outer join, full outer join, and self-outer join, etc., are termed as types of joins in SQL Server.
Types of Joins in SQL Server
There are different types of Joins
1. CROSS JOIN
2. INNER JOIN
3. OUTER JOIN
- LEFT OUTER JOIN
- RIGHT OUTER JOIN
4. FULL OUTER JOIN
5. SELF JOIN
Let us understand each of these joins in detail:-
1. Cross Join
- 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
Example
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.
2. Inner Join
Query output:
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.
Example
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
Query Output:
So we only get the data where there is a match between the two columns in each of the tables
3. Outer Join
- 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
Example
Query Output:
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
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 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
ExampleQuery Output:
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
Example
Query Output:
We get to combine the result of LEFT OUTER JOIN and RIGHT OUTER JOIN
5. Self 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
Example
SELECT *
FROM orders
JOIN subscriptions
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.
Conclusion
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.
Recommended Articles
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 –
7 Online Courses | 8 Hands-on Projects | 73+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses