Professional Documents
Culture Documents
Background
I'm a pretty visual person. Things seem to make more sense as a picture. I looked all over the
Internet for a good graphical representation of SQL JOINs, but I couldn't find any to my liking. Some
had good diagrams but lacked completeness (they didn't have all the possible JOINs), and some
were just plain terrible. So, I decided to create my own and write an article about it.
1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. OUTER JOIN
5. LEFT JOIN EXCLUDING INNER JOIN
6. RIGHT JOIN EXCLUDING INNER JOIN
7. OUTER JOIN EXCLUDING INNER JOIN
For the sake of this article, I'll refer to 5, 6, and 7 as LEFT EXCLUDING JOIN, RIGHT EXCLUDING
JOIN, and OUTER EXCLUDING JOIN, respectively. Some may argue that 5, 6, and 7 are not really
joining the two tables, but for simplicity, I will still refer to these as Joins because you use a SQL Join
in each of these queries (but exclude some records with a WHERE clause).
Inner JOIN
This is the simplest, most understood Join and is the most common. This query will return all of the
records in the left table (table A) that have a matching record in the right table (table B). This Join is
written as follows:
Left JOIN
This query will return all of the records in the left table (table A) regardless if any of those records
have a match in the right table (table B). It will also return any matching records from the right table.
This Join is written as follows:
Right JOIN
This query will return all of the records in the right table (table B) regardless if any of those records
have a match in the left table (table A). It will also return any matching records from the left table.
This Join is written as follows:
Outer JOIN
This Join can also be referred to as a FULL OUTER JOIN or a FULL JOIN. This query will return all of
the records from both tables, joining records from the left table (table A) that match records from
the right table (table B). This Join is written as follows:
This query will return all of the records in the left table (table A) that do not match any records in the
right table (table B). This Join is written as follows:
Collapse | Copy Code
SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL
This query will return all of the records in the right table (table B) that do not match any records in
the left table (table A). This Join is written as follows:
This query will return all of the records in the left table (table A) and all of the records in the right
table (table B) that do not match. I have yet to have a need for using this type of Join, but all of the
others, I use quite frequently. This Join is written as follows:
Examples
Suppose we have two tables, Table_A and Table_B. The data in these tables are shown below:
TABLE_B
PK Value
---- ----------
1 TROT
2 CAR
3 CAB
6 MONUMENT
7 PC
8 MICROSOFT
9 APPLE
11 SCOTCH
(5 row(s) affected)
Collapse | Copy Code
-- LEFT JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK
(8 row(s) affected)
Collapse | Copy Code
-- RIGHT JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK
(8 row(s) affected)
Collapse | Copy Code
-- OUTER JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
(3 row(s) affected)
Collapse | Copy Code
-- OUTER EXCLUDING JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL
OR B.PK IS NULL
(6 row(s) affected)
Note on the OUTER JOIN that the inner joined records are returned first, followed by the right joined
records, and then finally the left joined records (at least, that's how my Microsoft SQL Server did it;
this, of course, is without using any ORDER BY statement).
You can visit the Wikipedia article for more info here (however, the entry is not graphical).
I've also created a cheat sheet that you can print out if needed. If you right click on the image below
and select "Save Target As...", you will download the full size image.
History
Initial release -- 02/03/2009.
Version 1.0 -- 02/04/2009 -- Fixed cheat sheet and minor typos.
***************END********************************
I generally feel that people are afraid of Joins in SQL Server. But if they know what the different
types of Joins in SQLServer are and how they can be best used then they really enjoy using them. By
using the joins we can get data from many tables based on some logical conditions.
Let's suppose we have two tables Employee and Department whose description is given below:-
After the creation of the tables we need to insert the data into these tables. To insert the data the
following queries are used:-
Now, if we want to get employee id, employee first name, employee's last name and their
department name for those entries employee which belongs to at least one department, then we can
use the inner join.
Result
Collapse | Copy Code
Explanation
In this query, we used the inner join based on the column "Departmentid" which is common in both
the tables "Employee" and "Department". This query will give all the rows from both the tables which
have common values in the column "Departmentid". Neha Sharma and Samir Singh has the value "2"
in the Departmentid column of the table Employee. In the Department table, the Department
"Admin" has the value "2" in the Departmentid column. Therefore the above query returns two rows
for the department "Admin", one for Neha Sharma and another for Samir Singh.
Self Join
Sometime we need to join a table to itself. This type of join is called Self join. It is one of the type of
inner join where both the columns belong to the same table. In this Join, we need to open two
copies of a same table in the memory. Since the table name is the same for both instances, we use
the table aliases to make identical copies of the same table to be open in different memory locations.
For example if we need to get the employee name and their manager name we need to use the self
join, since the managerid for an employee is also stored in the same table as the employee.
Result
Collapse | Copy Code
Explanation
Since the employee and the manager information is contained in the same table (Employee, since
both are employees), we have to use the Self Join. In the self join query, we make two copies of the
table Employee by using the aliases Emp1 and Emp2 and then use Inner join between them by using
the managerid column of the Emp1 and Empid column of the table Emp2.In this example, we use
managerid and empid columns of the Employee table since the employee id of the manager of an
employee is stored in the managerid of the Employee table.
Outer Join
This type of join is needed when we need to select all the rows from the table on the left (or right or
both) regardless of whether the other table has common values or not and it usually enter null values
for the data which is missing.
Result
Collapse | Copy Code
Explanation
Since we have use the Left Outer Join, this query will give the information (Employee id, Employee
first name, Employee last name and their department name) for all the employee from the Employee
table and it insert NULL value in the DepartmentName column where the employee does not belong
to any department. In the table Employee, since Samir Singh, Amit Kumar and Neha Sharma have
values in their Departmentid column, therefore the above query will display their Department name
under the heading DepartmentName.But since Vivek Kumar doesn't belongs to any department and
has null value in the column Departmentid therefore the above query will Display the NULL value
under the column heading DepartmentName.
SELECT Dept.DepartmentName,
Emp.Empid, Emp.EmpFirstName,
Emp.EmpLastName
FROM Employee Emp
RIGHT OUTER JOIN Department dept
ON Emp.Departmentid=Dept.Departmentid
Result
Collapse | Copy Code
DepartmentName Empid EmpFirstName EmpLastName
Accounts 2 Amit Kumar
Admin 1 Samir Singh
Admin 3 Neha Sharma
HR NULL NULL NULL
Technology NULL NULL NULL
Explanation
Since we have use the Right Outer Join, this query will join the two tables Employee and Department
on the basis of the values contains in the column Departmenttid. It will give the department name
from the Department table and the Employee id, Employee first name, and Employee last name of all
the employees that belong to that department. If any department does not contain any employee
then it insert NULL value in the columns coming from the Employee table. Since no employee is
connected to the departments HR and Technology, this query will display NULL values under the
columns Empid, EmpFirstName and EmpLastName for the Departments HR and Technology. Since
the department Admin and Accounts contains the employees therefore the columns Empid,
EmpFirstName and EmpLastName contains the information, employee id, employee first name and
employee last name respectively.
SELECT Emp.Empid,
Emp.EmpFirstName,
Emp.EmpLastName,
Dept.DepartmentName
FROM Employee Emp
FULL OUTER JOIN Department dept
ON Emp.Departmentid=Dept.Departmenttid
Result
Collapse | Copy Code
Since we have used the Full Outer Join, this query will give the name of all the departments from the
Department table and the Employee id, Employee first name, Employee last name of all the
employees from the Employee table. If any department does not contain any employee, then it insert
NULL value in the columns Empid, EmpFirstName, EmpLastName columns and if any employee
doesn't belong to any department then it insert NULL value in the column DepartmentName. Here
since Vivek Kumar doesn't belong to any department, the result displays NULL value under the
column DepartmentName. Since the departments HR and Accounts don't contain any employees,
the result of the above query displays NULL values under the columns Empid, EmpFirstName and
EmpLastName for the departments HR and Technology..
Cross Join
This join combines all the rows from the left table with every row from the right table. This type of
join is needed when we need to select all the possible combinations of rows and columns from both
the tables. This type of join is generally not preferred as it takes lot of time and gives a huge result
that is not often useful.
SELECT Emp.Empid,
Emp.EmpFirstName,
Emp.EmpLastName,
Dept.DepartmentName
FROM Employee Emp
CROSS JOIN Department dept
Results
Collapse | Copy Code
This Cross Join query will give combines all the rows from the Employee table with every row of the
Department table. Since the Employee table contains 4 rows and the Department table contains 4
rows, therefore this result will returns 4*4=16 rows. This query doesn't contain any ON clause.