Sei sulla pagina 1di 6

11/30/2014

Types of Join in SQL Server - CodeProject


11,052,364 members (67,766 online)

home

articles

quick answers

discussions

Sign in

features

community

help

Search for articles, questions, tips

Articles Database Database SQL Server

Types of Join in SQL Server


Nirav Prabtani, 20 Jan 2014

CPOL

Info
Rate this:

4.83 (16 votes)

Types of join in SQL Server for fetching records from multiple tables.

First Posted

20 Jan 2014

Views

59,997

Bookmarked

28 times

Introduction
In this tip, I am going to explain about types of join.

What is join??
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
There are many types of join.
Inner Join
1. Equi-join
2. Natural Join
Outer Join
1. Left outer Join
2. Right outer join
3. Full outer join
Cross Join
Self Join

Using the Code


Join is very useful to fetching records from multiple tables with reference to common column between them.
To understand join with example, we have to create two tables in SQL Server database.
1. Employee

Collapse | Copy Code

create table Employee(


id int identity(1,1) primary key,
Username varchar(50),
FirstName varchar(50),
LastName varchar(50),
DepartID int
)

2. Departments

Collapse | Copy Code

create table Departments(


id int identity(1,1) primary key,
DepartmentName varchar(50)
)

Now fill Employee table with demo records like that.

http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

1/6

11/30/2014

Types of Join in SQL Server - CodeProject

Fill Department table also like this....

1) Inner Join
The join that displays only the rows that have a match in both the joined tables is known as inner join.
Collapse | Copy Code

select e1.Username,e1.FirstName,e1.LastName,e2.DepartmentName _
from Employee e1 inner join Departments e2 on e1.DepartID=e2.id

It gives matched rows from both tables with reference to DepartID of first table and id of second table like this.

Tip/Trick
Browse Code
Stats
Revisions (3)
Alternatives
Comments (7)

Research

Equi-Join
Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for
join using equality operator, then that join query comes under Equi join.
Equi join has only (=) operator in join condition.
Equi join can be inner join, left outer join, right outer join.
Check the query for equi-join:

Tagged as
SQL

Cutting Costs and


Boosting
Productivity for...
Realize the Power
of Connected
Intelligence...

Collapse | Copy Code

SELECT * FROM Employee e1 JOIN Departments e2 ON e1.DepartID = e2.id

Related Articles
Visual
Representation of
SQL Joins
SQL Joins
Generate Data
Dictionary from SQL
Server
Understanding Table
Joins using SQL
Learn SQL to LINQ
(Visual
Representation)

2) Outer Join
Outer join returns all the rows of both tables whether it has matched or not.
We have three types of outer join:
1. Left outer join
2. Right outer join
3. Full outer join
a) Left Outer join

http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

2/6

11/30/2014

Types of Join in SQL Server - CodeProject


Left join displays all the rows from first table and matched rows from second table like that..
Collapse | Copy Code

SELECT * FROM Employee e1 LEFT OUTER JOIN Departments e2


ON e1.DepartID = e2.id

Result:
Go to top

b) Right outer join


Right outer join displays all the rows of second table and matched rows from first table like that.
Collapse | Copy Code

SELECT * FROM Employee e1 RIGHT OUTER JOIN Departments e2


ON e1.DepartID = e2.id

Result:

3) Full outer join


Full outer join returns all the rows from both tables whether it has been matched or not.
Collapse | Copy Code

SELECT * FROM Employee e1 FULL OUTER JOIN Departments e2


ON e1.DepartID = e2.id

Result:

3) Cross Join
A cross join that produces Cartesian product of the tables that are involved in the join. The size of a Cartesian
product is the number of the rows in the first table multiplied by the number of rows in the second table like this.
Collapse | Copy Code

SELECT * FROM Employee cross join Departments e2

You can write a query like this also:

http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

3/6

11/30/2014

Types of Join in SQL Server - CodeProject


Collapse | Copy Code

SELECT * FROM Employee , Departments e2

4) Self Join
Joining the table itself called self join. Self join is used to retrieve the records having some relation or similarity with
other records in the same table. Here, we need to use aliases for the same table to set a self join between single
table and retrieve records satisfying the condition in where clause.
Collapse | Copy Code

SELECT e1.Username,e1.FirstName,e1.LastName from Employee e1 _


inner join Employee e2 on e1.id=e2.DepartID

Here, I have retrieved data in which id and DepartID of employee table has been matched:

Points of Interest
Here, I have taken one example of self join in this scenario where manager name can be retrieved by managerid
with reference of employee id from one table.
Here, I have created one table employees like that:

If I have to retrieve manager name from manager id, then it can be possible by Self join:
Collapse | Copy Code

select e1.empName as ManagerName,e2.empName as EmpName _


from employees e1 inner join employees e2 on e1.id=e2.managerid

Result:

History
20 Jan 2014: Initial post

License
This article, along with any associated source code and files, is licensed under The Code Project Open License
(CPOL)

Share
http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

4/6

11/30/2014

Types of Join in SQL Server - CodeProject


EMAIL

About the Author


Nirav Prabtani
Web Developer Satva Infotech
India

Nirav Prabtani
I am a software engineer at Satva Infotech, Database Architect and Designer /Technical Architect/Analyst
Programmer in Microsoft .NET Technologies & Microsoft SQL Server with more than
2 years of hands on experience.

I love to code....!!!
My recent past includes my work with the education domain as a technical business
requirement analyst, database architect & designer and analyst programmer; just
love my involvement with the world of knowledge, learning and education and I think
I know quite well what I want to do in life & in my career. What do I like? Well,
ideation, brainstorming, coming up with newer and more creative ways of doing things;
each time with an enhanced efficiency. An item in my day's agenda always has a task
to look at what I did yesterday & focus on how I can do it better today

Contact Me

Nirav Prabtani

Mobile : +91 738 308 2188


Email : niravjprabtani@gmail.com
My Blog:
Nirav Prabtani

Follow on

Twitter

LinkedIn

http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

5/6

11/30/2014

Types of Join in SQL Server - CodeProject

Comments and Discussions


You must Sign In to use this message board.
Search Comments
Profile popups Spacing Relaxed

Noise Medium

Go

Layout Normal

Per page 25

Update

First Prev Next

Adition things About Natual join.

ashu_om

10-Nov-14 19:02

My vote of 5

Sibeesh KV

19-Sep-14 20:43

My vote of 5

Animesh Datta

21-May-14 23:13

Nirav Prabtani

26-May-14 21:49

Join in sql server

Member 9954224

24-Apr-14 6:53

Some points

Christian Graus

Re: My vote of 5

Re: Some points

News

21-Jan-14 3:34

Nirav Prabtani

Last Visit: 31-Dec-99 19:00


General

20-Jan-14 15:17

Last Update: 29-Nov-14 15:06


Suggestion

Question

Refresh
Bug

Answer

Joke

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
Web02 | 2.8.1411028.1 | Last Updated 20 Jan 2014

Select Language

http://www.codeproject.com/Tips/712941/Types-of-Join-in-SQL-Server

Layout: fixed | fluid

Article Copyright 2014 by Nirav Prabtani


Everything else Copyright CodeProject, 1999-2014

6/6