SQL Server-4 - JOINS
SQL Server-4 - JOINS
Again the ANSI format joins classified into three types such as
1. Inner join
2. Outer join
3. Cross join
Further, the outer join is divided into three types are as follows
1. Left outer join
2. Right outer join
3. Full outer join
NON-ANSI join in SQL Server are classified into four types such as
1. EQUI join
2. NON-EQUI join
3. SELF-join
4. Natural Join
Following is the SQL Server Inner Join Query that is joining the Employee and Projects
tables and gives you the result set as shown in the above image. Here, we are using the
EmployeeId column to check the similar values on the ON clause as both the tables having
this column (Id in Employee table and EmployeeId in Projects table).
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
INNER JOIN Projects
ON Employee.Id = Projects.EmployeeId;
If you look at the output, here, we got 8 rows. We did not get those 3 records that have the
NULL value in the EmployeeId column of the Projects table. So, this proves that the Inner
Join in SQL Server is used to return only the matching records from both the tables involved
in the join. The non-matching records are simply ignored or eliminated.
Instead of using the INNER JOIN keyword, we can also use the JOIN keyword as shown in
the below Query, and we will also get the same output as the previous example. This also
proofs that the default joins in SQL Server is Inner Join.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
JOIN Projects
ON Employee.Id = Projects.EmployeeId;
Now using SQL Server LEFT OUTER JOIN, we can join the Employee and Projects table
and display the information of the employees and the projects they are working on, we also
get the details of employees who are not working on any project and in that case, the
Project and ClientId column will take NULL values. The Following Query will give you the
results as shown in the above image.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
LEFT OUTER JOIN Projects
ON Employee.Id = Projects.EmployeeId;
If you look at the output, here, we got all 10 records (i.e. all the records from
the Employee Table) including the row that has a null value for the EmployeeId column in
the Projects table. So, this proofs that the Left Outer Join will retrieve all the rows from
the Left-hand side Table including the rows that have a null foreign key value in the right-
hand side table.
Instead of using the LEFT OUTER JOIN keyword, you can also use
the LEFT JOIN keyword as shown in the below Query, and also you will get the same
output as the previous query.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
LEFT JOIN Projects
ON Employee.Id = Projects.EmployeeId;
Now using SQL Server RIGHT OUTER JOIN, we can join Employee and Projects table and
display the information of employees and the projects they are working on, we also get the
details of projects which are not yet assigned to any employee. The Following Query will
give you the results as shown in the above image.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
RIGHT OUTER JOIN Projects
ON Employee.Id = Projects.EmployeeId;
If you look at the output, here, we got all 11 rows (i.e. all the rows from the Projects Table).
If you further notice, here, we got all the matching records from both the Employee and
Projects tables as well as all non-matching rows from the right-side table i.e. the Projects
Table. So, this proofs that the Right Outer Join retrieves all the rows from the Left-hand side
Table including the rows that have a null foreign key value in the left-hand side table.
Instead of using the RIGHT OUTER JOIN keyword, we can also use
the RIGHT JOIN keyword as shown in the below SQL Query, and also get the same output
as the previous query.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
RIGHT JOIN Projects
ON Employee.Id = Projects.EmployeeId;
Now using SQL Server FULL OUTER JOIN, we can join Employee and Projects table and
display the information of employees and the projects they are working on, we also get the
details of projects which are not yet assigned to any employee as well as all the employees
who are not assigned with any projects. The Following Query will give you the results as
shown in the above image.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
FULL OUTER JOIN Projects
ON Employee.Id = Projects.EmployeeId;
If you look at the output, here, we got all 13 rows (i.e. all the rows from the Projects Table
as well as all the rows from the Employee table). Instead of using the FULL OUTER
JOIN keyword, we can also use the FULL JOIN keyword as shown in the below Query, and
get the same output as the previous query.
SELECT Id as EmployeeID, Name, Department, City, Title as Project, ClientId
FROM Employee
FULL JOIN Projects
ON Employee.Id = Projects.EmployeeId;
The Inner Join in SQL Server is used to returns only the matching rows from both the tables
involved in the join by removing the non-matching rows. That means Inner join results only
the matching rows from both the tables involved in the join. The following diagram shows
the pictorial representation of Inner Join.
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId;
If you look at the above output, we got only 5 rows. We did not get the 2 rows that have the
NULL value in the CompanyId column. Instead of using the INNER JOIN keyword, we can
also use the JOIN keyword as shown in the below SQL Query. JOIN or INNER JOIN means
the same. But it’s always better to use INNER JOIN, as this explicitly specifies your
intention.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
So, in short, we can say that the Inner Join is used to returns only the matching records
from both the tables involved in the join. The non-matching rows are simply eliminated.
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName,
Addr.Country,
Addr.State,
Addr.City
ON Cand.CompanyId = Comp.CompanyId
ON Addr.CandidateId = Cand.CandidateId;
When you execute the above query, you will get the following output. Now you can see, we
got only three records, this is because the following three records exist in all the three
tables.
It is also possible to use Different types of Joins while joining three tables in SQL Server.
For example, in the below SQL Query, we perform the first join (between Candidate and
Company) using Inner JOIN and the second join (between Candidate and Address)
operation using LEFT JOIN.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName,
Addr.Country,
Addr.State,
Addr.City
ON Cand.CompanyId = Comp.CompanyId
ON Addr.CandidateId = Cand.CandidateId;
When you execute the above SQL Query, you will get the following output. In this case, for
the left join between Candidate and Campany, it will take null values for Country, State, and
City column which does not have any reference.
Note: You need to use Inner JOIN when you want to retrieve only the matching records
from both the tables involved in the JOIN by eliminating the non-matching records from both
the tables. In most real-time applications, we need to use this JOIN
The Left Outer Join in SQL Server is used to retrieve the matching records from both the
tables as well as the non-matching records from the left side table involved in the JOIN. In
that case, the un-matching data will take the null value. If this is not clear at the moment
then don’t worry we will see it with an example. The following diagram shows the pictorial
representation of the Left Outer Join.
The question that should come to your mind is which is the left table and which is the right
table? The answer is, the table which is mentioned to the left of the LEFT OUTER JOIN
keyword is the left table, and the table which is mentioned to the right of the LEFT OUTER
JOIN keyword is the right table.
How to implement Left Outer Join in SQL Server?
Please have a look at the following image which shows the syntax of Left Outer Join in SQL
Server. Here, you can use either the Left Outer Join or the Left Join keywords to perform
the Left Outer Join operation. If you use only the Left JOIN keyword, then also it is going to
perform the Left Outer Join operation.
When do we need to use Left JOIN or Left Outer Join in SQL Server?
If you want to select all the records from the Left side table including the records that have a
null foreign key value then you need to use the SQL Server Left Outer Join. To simplify the
above definition, we can say that if you want to retrieve all the matching records from both
the tables involved in the join as well as the non-matching records from the left side table
then we need to use Left Join or Left Outer Join in SQL Server.
Following is the SQL Query which is an example of Left Outer Join that is joining the
Company and Candidate tables and will give the results shown in the above image.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId;
If you look at the output, here, we got all 7 rows (i.e. all the rows from the Candidate Table)
including the row that has a null value for the CompanyId column. So this proofs that the
SQL Server Left Outer Join will retrieve all the rows from the Left-hand side Table including
the rows that have a null foreign key value.
Instead of using the Left Outer Join keyword, we can also use the Left Join keyword. This
will also work as expected as Left Outer Join. That means the Left Outer Join or Left Join
means the same. Following is the SQL Query that uses Left Join Keyword to join Candidate
and Company tables.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId;
In short, we can say that Left Outer Join will return all the matching records from both the
tables involved in the join as well as all the non-matching records from the left-hand side
table. Now, let us proceed and understand some tricky questions which may be asked in
SQL Server interviews.
How to retrieve only the non-matching rows from the left table in SQL
Server?
Here, what we want is, we want to retrieve only the non-matching records from the left
table. For example, we want the following result sets when we join the Candidate and
Company tables. If you further notice, here we want to retrieve those rows from the
Candidate table whose does not have any reference in the Company table.
As we already discussed, the Left Outer Join is used to fetch all the matching records from
both the tables as well as all the non-matching records from the left-hand side table. In this
case, the non-matching records will take NULL value. So, to get the above output what we
need to do is, we need to perform Left Outer Join, and then in the where condition we need
to filter out the records which have NULL values. The following Left OuterJoin Query with
the where clause does the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName,
Addr.Country,
Addr.State,
Addr.City
FROM Candidate Cand
ON Cand.CompanyId = Comp.CompanyId
ON Addr.CandidateId = Cand.CandidateId;
When you execute the above SQL Query, you will get the following output.
Note: You need to use LEFT OUTER JOIN when you want to retrieve all the matching
records from both the tables involved in the JOIN as well as non-matching records from the
left-hand side table.
The Right Outer Join in SQL Server is used to retrieve all the matching records from both
the tables involved in the join as well as all the non-matching records from the right-hand
side table. In that case, the un-matching data will take the null value. The following diagram
shows the pictorial representation of the Right Outer Join in SQL Server.
The question that should come to your mind is which is the left table and which is the right
table? The answer is, the table which is mentioned to the left of the RIGHT OUTER JOIN
keyword is the left table, and the table which is mentioned to the right of the RIGHT OUTER
JOIN keyword is the right table. The following diagram shows the pictorial representation of
Right Outer Join.
Following is the Query which is an example of Right Outer Join that is joining the Company
and Candidate tables and will give the results shown in the above image.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
If you look at the above, we got 6 rows. We got all the matching rows from both the tables
as well as non-matching rows from the right side table i.e. the Company Table. Instead of
using the Right Outer Join keyword, you can also use the Right Join keyword. This will
also work as expected as Right Outer Join. That means the SQL Server Left Outer Join or
Left Join means the same. The following example uses the Right Join keyword and we also
get the same output as the previous example.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
In short, we can say that Right Outer Join will return all the matching records from both the
tables as well as all the non-matching records from the right-hand side table.
How to retrieve only the non-matching rows from the right table?
Here, what we want is, we want to retrieve only the non-matching records from the right
table. For example, we want the following result sets when we join the Candidate and
Company tables. If you further notice, here we want to retrieve those rows from the
Candidate table whose does not have any reference in the Company table.
As we already discussed, the Right Outer Join is used to fetch all the matching records from
both the tables as well as all the non-matching records from the right-hand side table. In this
case, the non-matching records will take NULL value. So, to get the above output what we
need to do is, we need to perform the Right Outer Join operation, and then in the where
condition we need to filter out the records which have NULL values. The following Right
OuterJoin Query with the where clause does the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName,
Addr.Country,
Addr.State,
Addr.City
ON Cand.CompanyId = Comp.CompanyId
ON Addr.CandidateId = Cand.CandidateId;
When you execute the above Right Join SQL Query, you will get the following output.
5.Full Outer Join in SQL Server
The Full Outer Join in SQL Server is used to retrieve all the matching records from both the
tables involved in the join as well as all the non-matching records from both the tables. The
Un-matching data in such cases will take the null value. The following diagram shows the
pictorial representation of Full Outer Join in SQL Server.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
If you look at the output, now we now got 8 rows. That means all the records from the
Candidate Table as well as all the records from the Company Table. Instead of using the
Full Outer Join keyword, you can also use the Full Join keyword. This will also work as
expected as Full Outer Join. That means the SQL Server Full Outer Join or Full Join means
the same. The following example uses Full Join Keyword to join both tables.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
ON Cand.CompanyId = Comp.CompanyId
In short, Full Outer Join, or you can say Full Join will return all the records from both left and
right tables involved in the join, including the non-matching rows.
How to retrieve only the non-matching rows from both the left and right
table?
Here, what we want is, we want to retrieve only the non-matching records from both tables.
For example, we want the following result sets when we join the Candidate and Company
tables. If you further notice, here all the matching rows are eliminated.
As we already discussed, the Full Outer Join is used to fetch all the matching records from
both the tables as well as all the non-matching records from both the table. In this case, the
non-matching records will take NULL value. So, to get the above output what we need to do
is, we need to perform Full Outer Join operation, and then in the where condition we need
to filter out the records which have NULL values. The following Full OuterJoin Query with
the where clause does the same.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
OR Comp.CompanyId IS NULL
Cand.FullName,
Cand.CompanyId,
Comp.CompanyName,
Addr.Country,
Addr.State,
Addr.City
ON Cand.CompanyId = Comp.CompanyId
ON Addr.CandidateId = Cand.CandidateId;
When you execute the above Full Outer Join Query, you will get the below output.
The Self Join is nothing a concept where we need to join a table by itself. You need to use
Self Join when you have some relations between the columns of the same table. If this is
not clear at the moment then don’t worry we will discuss this with an example. I am sure at
the end of this article, you definitely have a very good idea of when to and how to use SQL
Server Self Join.
The point that you need to remember that when you are implementing the self-join
mechanism then you need to create the alias for the table. You can also create any number
of aliases for a single table in SQL Server. The Self Join is not a different type of join. It can
be classified as any type of join, such as
1. Inner Join
2. Outer (Left, Right, Full) join
3. Cross Join
The Self Join is like any other join except that the two instances of the same table will be
joined in the query.
Let’s understand Self Join with Examples
We are going to use the following Employee table to understand the SQL Server Self Join
concept.
FROM Employee E
ON E.ManagerId = M.EmployeeId
ON E.ManagerId = M.EmployeeId
When we execute the above query it produces the output as shown below.
FROM Employee E
A Cross Join produces the Cartesian product of the tables involved in the join. The
Cartesian product means the number of records present in the first table is multiplied by the
number of records present in the second table. Please have a look at the below SQL query
which will which is an example of Cross Join for joining the Candidate and Company Table.
SELECT Cand.CandidateId,
Cand.FullName,
Cand.CompanyId,
Comp.CompanyId,
Comp.CompanyName
FROM Candidate Cand
CROSS JOIN Company Comp
When we execute the above cross join query, it produces the result set as shown in the
image below.