SQL Joins - CodeProject
SQL Joins - CodeProject
1 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
Sign in
home
articles
quick answers
discussions
features
community
help
SQL Joins
Vivek Johari, 20 Aug 2010
CPOL
Rate this:
6/11/2015 12:58 AM
2 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
Inner Join
This type of join is also known as the Equi join. This join returns all the rows from both tables where there is a match. This type of join can
be used in the situation where we need to select only those rows which have values common in the columns which are specified in the ON
clause.
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
Hide Copy Code
Empid
1
2
3
EmpFirstName
Samir
Amit
Neha
EmpLastName
Singh
Kumar
Sharma
DepartmentName
Admin
Accounts
Admin
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.
SELECT Emp1.Empid,
Emp1.EmpFirstName+' '+Emp1.EmpLastName as EmployeeName,
Emp2.EmpFirstName+' '+Emp2.EmpLastName as ManagerName
FROM Employee Emp1
INNER JOIN Employee Emp2
ON Emp1.Managerid=Emp2.Empid
Result
Hide Copy Code
Empid
1
2
3
4
EmployeeName
Samir Singh
Amit Kumar
Neha Sharma
Vivek Kumar
ManagerName
Amit Kumar
Samir Singh
Samir Singh
Samir Singh
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
6/11/2015 12:58 AM
3 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
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.
The Outer join can be of three types
1. Left Outer Join
2. Right Outer Join
3. Full Outer Join
SELECT Emp.Empid,
Emp.EmpFirstName,
Emp.EmpLastName,
Dept.DepartmentName
FROM Employee Emp
LEFT OUTER JOIN Department dept
ON Emp.Departmentid=Dept.Departmenttid
Result
Hide Copy Code
Empid
1
2
3
4
EmpFirstName
Samir
Amit
Neha
Vivek
EmpLastName
Singh
Kumar
Sharma
Kumar
DepartmentName
Admin
Accounts
Admin
NULL
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
6/11/2015 12:58 AM
4 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
Result
Hide Copy Code
DepartmentName
Accounts
Admin
Admin
HR
Technology
Empid
2
1
3
NULL
NULL
EmpFirstName
Amit
Samir
Neha
NULL
NULL
EmpLastName
Kumar
Singh
Sharma
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
Hide Copy Code
Empid
1
2
3
4
NULL
NULL
EmpFirstName
Samir
Amit
Neha
Vivek
NULL
NULL
EmpFirstName
Singh
Kumar
Sharma
Kumar
NULL
NULL
DepartmentName
Admin
Accounts
Admin
NULL
HR
Technology
Explanation
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
6/11/2015 12:58 AM
5 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
SELECT Emp.Empid,
Emp.EmpFirstName,
Emp.EmpLastName,
Dept.DepartmentName
FROM Employee Emp
CROSS JOIN Department dept
Results
Hide Copy Code
Empid
1
2
3
4
1
2
3
4
1
2
3
4
1
2
3
4
EmpFirstName
Samir
Amit
Neha
Vivek
Samir
Amit
Neha
Vivek
Samir
Amit
Neha
Vivek
Samir
Amit
Neha
Vivek
EmpLastName
Singh
Kumar
Sharma
Kumar
Singh
Kumar
Sharma
Kumar
Singh
Kumar
Sharma
Kumar
Singh
Kumar
Sharma
Kumar
DepartmentName
Accounts
Accounts
Accounts
Accounts
Admin
Admin
Admin
Admin
HR
HR
HR
HR
Technology
Technology
Technology
Technology
Explanation
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.
Wrapping Up
The above discussion can be summarized as joins are used to select data from more than one table in a single query. The inner join is
used to select only those rows that have common values in the column on which join is based. The Left Outer Join is used to select the
rows from the left hand side table regardless of whether the table on the right hand side has common values or not. Similarly the Right
Outer join is used to select rows from the table on the right hand side regardless of whether the table on the left hand side has common
values or not. The Cross join is used to get rows from all the possible combinations of rows and columns from both the table. If should
be used when it the only way left since it may run for a very long time and returns a huge result set which may not be useful.
Joining of the tables should be avoided if it is based on the columns that have very few unique values. To increase the JOIN performance
it is better to limits the number of rows needed to be joined, by including a WHERE clause in your query. Join performance can also we
increased if the columns used for joining the tables have their own indexes.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Share
EMAIL
6/11/2015 12:58 AM
6 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
Vivek Johari
Technical Lead
India
I am currently working as a Analyst and have around 7.5 years of experience in database.
Degree:Master Degree in Computer(MCA)
Work experience:Designing of the database.
Database Optimization.
Writing Complex Stored Procedures,Functions,Triggers etc.
Designing and developing SSIS & DTS packages.
Designing SQL Reports using SSRS.
Database Server Maintenance.
Certification:Microsoft certified Sql DBA in Sql server 2008 (MCTS).
Microsoft certified BI professional in Sql server 2008 (MCTS).
Oracle certified profession DBA in ORACLE 10g (OCP)
certified profession DBA in ORACLE 9i (OCP)
My other publication
Technical Blog:- Technologies with Vivek Johari
Moderator and Blogger at BeyondRelational.com
Guest Author and Blogger at sqlservercentral.com
SQL Joins
Search Comments
Profile popups
Spacing Relaxed
Layout Normal
Per page 25
Update
6/11/2015 12:58 AM
7 of 7
http://www.codeproject.com/Articles/102805/SQL-Joins
My vote to you 5
sithmichel
3-Nov-15 21:31
My vote of 5
Sibeesh KV
19-Sep-14 20:43
Vivek Johari
24-Sep-14 23:13
Sibeesh KV
24-Sep-14 23:40
Member 10992162
4-Aug-14 22:40
Vivek Johari
4-Aug-14 22:44
Member 9861188
24-Sep-13 2:49
Re: My vote of 5
Re: My vote of 5
it was very useful
Re: it was very useful
Very Nice Article
Re: Very Nice Article
Vivek Johari
Vivek Johari
19-Sep-13 6:05
24-May-13 9:42
9-Sep-13 2:45
Member 10066698
20-May-13 22:27
Vivek Johari
My vote of 1
9-Sep-13 2:46
SalCon
Re: My vote of 1
18-Mar-13 23:09
Vivek Johari
My vote of 5
20-Mar-13 1:18
jerrymillerjr
Re: My vote of 5
10-Dec-12 11:57
Vivek Johari
20-Mar-13 1:19
shailendraturankar
14-Apr-12 17:31
Vivek Johari
My vote of 4
Re: My vote of 4
My vote of 5
Re: My vote of 5
26-Jun-12 6:58
Itz.Irshad
5-Dec-11 22:49
Vivek Johari
5-Dec-11 23:50
ankush teotia
21-Oct-11 1:05
Vivek Johari
News
19-Sep-13 1:53
Vivek Johari
General
SOMASUNDARAM
SUBRAMANIAN
Arpit Mandliya
Re: My vote of 5
24-Sep-13 19:13
3-Nov-11 2:23
swapnat
Suggestion
Question
31-Jan-11 13:30
Refresh
Bug
Answer
Joke
Praise
1 2 Next
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151103.1 | Last Updated 20 Aug 2010
Select Language
Layout: fixed |
fluid
6/11/2015 12:58 AM