10
Search for articles, questions, tips Q&A forums lounge Rate this: Nirav Prabtani, 20 Jan 2014 CPOL Types of Join in SQL Server Types of join in SQL Server for fetching records from multiple tables. 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. 4.81 ﴾23 votes﴿ 11,561,474 members ﴾48,592 online﴿ Sign in articles

JOINS SQL.pdf

Embed Size (px)

DESCRIPTION

SQL Joins

Citation preview

Page 1: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 1/10

Search for articles, questions, tipsQ&A forums lounge

Rate this:Nirav Prabtani, 20 Jan 2014 CPOL

Types of Join in SQL Server

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

IntroductionIn 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 betweenthem.

There are many types of join.

Inner Join

1. Equi‐join2. Natural Join

Outer Join

1. Left outer Join2. Right outer join3. Full outer join

Cross JoinSelf Join

Using the CodeJoin is very useful to fetching records from multiple tables with reference to common column betweenthem.

   4.81 ﴾23 votes﴿

11,561,474 members ﴾48,592 online﴿ Sign in

articles

Page 2: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 2/10

To understand join with example, we have to create two tables in SQL Server database.

1. EmployeeHide   Copy Code

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

2. DepartmentsHide   Copy Code

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

Now fill Employee table with demo records like that.

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.

Hide   Copy Code

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

Page 3: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 3/10

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

Equi‐Join

Equi join is a special type of join in which we use only equality operator. Hence, when you make a query forjoin 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:

Hide   Copy Code

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

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 join2. Right outer join3. Full outer join

a﴿ Left Outer join

Left join displays all the rows from first table and matched rows from second table like that..

Hide   Copy Code

 SELECT * FROM Employee e1 LEFT OUTER JOIN Departments e2

Page 4: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 4/10

ON e1.DepartID = e2.id 

Result:

b﴿ Right outer join

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

Hide   Copy Code

 SELECT * FROM Employee e1 RIGHT OUTER JOIN Departments e2ON 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.

Hide   Copy Code

 SELECT * FROM Employee e1 FULL OUTER JOIN Departments e2ON e1.DepartID = e2.id 

Result:

Page 5: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 5/10

3﴿ Cross Join

A cross join that produces Cartesian product of the tables that are involved in the join. The size of a Cartesianproduct is the number of the rows in the first table multiplied by the number of rows in the second table likethis.

Hide   Copy Code

 SELECT * FROM Employee cross join Departments e2 

You can write a query like this also:

Hide   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 orsimilarity with other records in the same table. Here, we need to use aliases for the same table to set a selfjoin between single table and retrieve records satisfying the condition in where clause.

Hide   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:

Page 6: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 6/10

Points of InterestHere, I have taken one example of self join in this scenario where manager name can be retrieved bymanagerid 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:

Hide   Copy Code

 select e1.empName as ManagerName,e2.empName as EmpName _from employees e1 inner join employees e2 on e1.id=e2.managerid 

Result:

History20 Jan 2014: Initial post

LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License﴾CPOL﴿

Share

EMAIL TWITTER

Page 7: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 7/10

About the Author

Nirav Prabtani Web Developer Satva Infotech

India

Nirav Prabtani

I am a software engineer at Satva Infotech, Database Architect and Designer /TechnicalArchitect/AnalystProgrammer in Microsoft .NET Technologies & Microsoft SQL Server with more than2 years of hands on experience.

I love to code....!!!

My recent past includes my work with the education domain as a technical businessrequirement analyst, database architect & designer and analyst programmer; justlove my involvement with the world of knowledge, learning and education and I thinkI 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 taskto look at what I did yesterday & focus on how I can do it better today

Contact Me

Page 8: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 8/10

Search Comments   Go

 

You may also be interested in...

SQL Joins Value of Database Resilience:Comparing Costs of Downtimefor IBM DB2 10.5 and MicrosoftSQL Server 2014

Visual Representation of SQLJoins

Understanding the Need for aSingle Code Base in EnterpriseMobile App Development

SQL Joins 6 Productivity Hacks for theEnterprise Developer

Comments and Discussions

You must Sign In to use this message board.

First Prev Next

Nirav Prabtani

Mobile : +91 738 308 2188

Email : [email protected]

My Blog:Nirav Prabtani

Adition things About Natual join.ashu_om 10‐Nov‐14 18:02

Page 9: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 9/10

Thanks Nirav for simple description , i wann add about natual join few bit on the same aritical.

Natural Join : SELECT * FROM Emp NATURAL JOIN Dept ‐‐ it implicitly compar by Dept_id because the commoncolumn in the table

Natural join is a type of equi join which occurs implicitly by comparing all the same names columns inboth tables. The join result have only one column for each pair of equally named columns.

Ashutosh SoniSoftware EngineerMumbai﴾India﴿

Sign In · View Thread · Permalink

5

Sign In · View Thread · Permalink 5.00/5 ﴾1 vote﴿

Nice Tip ... but where is the example﴾output﴿ of Cross Join .

Sign In · View Thread · Permalink 5.00/5 ﴾1 vote﴿

thank you for +5 and i don't think there is need of example for cross join but you can see theexample at..

Cross join

Sign In · View Thread · Permalink

Join is used to combine two or more table in a single table on the basis of condition or withoutcondition.Suppose we have two tables

My vote of 5Sibeesh KV 19‐Sep‐14 19:43

My vote of 5Animesh Datta 21‐May‐14 22:13

Re: My vote of 5Nirav Prabtani 26‐May‐14 20:49

Join in sql serverMember 9954224 24‐Apr‐14 5:53

Page 10: JOINS SQL.pdf

28/06/2015 Types of Join in SQL Server  CodeProject

http://www.codeproject.com/Tips/712941/TypesofJoininSQLServer 10/10

Permalink | Advertise | Privacy | Terms of Use | Mobile Web03 | 2.8.150624.2 | Last Updated 20 Jan 2014 Select Language ▼

Article Copyright 2014 by Nirav PrabtaniEverything else Copyright © CodeProject, 1999‐2015

Layout: fixed | fluid

1. Employee – We have two field in this table ﴾Emp_Id,Emp_Name﴿2. Emp_Department – We have three filed in this table ﴾Id,Emp_id,Emp_dept﴿

If we want to fetch ﴾Emp_Dept with Emp_name﴿ than we have to use Join keyword in our query.I am ging to write some Example – I hope you will Like this.

http://nirajtiwari.com/join‐sql‐server‐types‐join/[^]

Sign In · View Thread · Permalink 1.00/5 ﴾7 votes﴿

A self join is an inner join, it's not really a join type. You should also mention that self joins can bequite expensive.

"SELECT * FROM Employee e1 JOIN Departments e2 ON e1.DepartID = e2.id " is just an inner join. Anequi join is not standard SQL and looks like this:

SELECT * FROM Employee e1,Departments e2 WHERE e1.DepartID = e2.id

It's VERY expensive, as the , operator does a cross join and then the WHERE clause filters it down.

Christian Graus

My new article series is all about SQL !!!

Sign In · View Thread · Permalink

Thank you very much for this points

Sign In · View Thread · Permalink

Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin   

Some pointsChristian Graus 20‐Jan‐14 14:17

Re: Some pointsNirav Prabtani 21‐Jan‐14 2:34