Click here to Skip to main content
15,881,248 members
Articles / Database Development / SQL Server / SQL Server 2008R2

Differences Between Various Joins in MS SQL Server

Rate me:
Please Sign up or sign in to vote.
4.96/5 (15 votes)
18 Aug 2014CPOL2 min read 25.3K   129   15   17
Here, we will see the basic differences or similarities between various joins of MS SQL Server.

Introduction

When working with SQL joins, we can chose among from various choices, depending on the requirement. Well, each join sequence may differ from one another, and serves a special purpose. But few among these join queries differ in syntax /performance but they produce the same thing. To avoid confusion, here we will see the basic differences and similarities between various joins of MS SQL Server.

Background

Let’s say we have two tables:

SQL
CREATE Table Student
(    
    Id BIGINT,
    Name VARCHAR(100)
);
SQL
CREATE Table StudentAge
(    
    Student_Id BIGINT,  --foreign key from Student tables Id column
    Name VARCHAR(100),
    Age INT
);

Yes, there is no real foreign key between them, but let’s say we populated these tables with relational data like:

SQL
INSERT INTO Student
VALUES
(1, 'Dipon'),
(2, 'Dip'),
(3, 'Dipa'),    -- no age
(4, 'Dipika'),  -- no age
(5, 'Shamim'),  -- has age more than once
(6, 'Shatu'),   -- same person twice
(6, 'Shatu')
SQL
INSERT INTO StudentAge
VALUES
(1, 'Dipon', 10),
(2, 'Dip', 20),
(5, 'Shamim', 40),  -- same person twice age
(5, 'Shamim', 50),
(6, 'Shatu', 50),   -- same age to more than one people
(7, 'Jamal', 50),   -- no Student
(8, 'Kamal', 60)    -- no Student

As we can see, considering these two tables, some of the rows have relational data and for few there is none.

Let’s Start Joining

Gentlemen like to do hand shake
Sometimes, they shake hand if they only know each other.
Sometimes, they do the same even if they don’t know each other.
Sometimes, one may feel embarrassed, if he does find anyone to shake hands with.

Shakes hand - join made between rows
Know each other - has any data relation
Embarrassment revealed - no row found to join, NULL

Regular Joins

Shake hand if they know each other, no embarrassment revealed.

Join: JOIN
SQL
/*Join: JOIN*/
/*does: Shakes hand if they know each other, no embarrassment revealed*/
SELECT *
    FROM Student
    JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
Join: INNER JOIN
SQL
/*Join: INNER JOIN*/
/*does: as JOIN*/
SELECT *
    FROM Student
    INNER JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
Join: WHERE
SQL
/*Join: WHERE*/
/*does: as JOIN*/
SELECT *
    FROM Student, StudentAge
    WHERE Student.Id = StudentAge.Student_Id;
CROSS APPLY
SQL
/*CROSS APPLY*/
/*does: as JOIN*/
SELECT *
    FROM Student
    CROSS APPLY(
        SELECT *
            FROM StudentAge
            WHERE Student.Id = StudentAge.Student_Id
    )StudentAges

They all produced the same thing:

Image 1

http://www.sqlfiddle.com/#!3/517ad/1

Left Joins

Each person shakes hands if they know each other, embarrassment revealed for the left man.

Join: LEFT JOIN
SQL
/*Join: LEFT JOIN*/
/*does: Each person shakes hand if they know each other, embarrassment revealed for the left man*/
SELECT *
    FROM Student
    LEFT JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
Join: LEFT OUTER JOIN
SQL
/*Join: LEFT OUTER JOIN*/
/*does: as LEFT JOIN*/
SELECT *
    FROM Student
    LEFT OUTER JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
OUTER APPLY
SQL
/*OUTER APPLY*/
/*does: as LEFT JOIN*/    
SELECT *
    FROM Student
    OUTER APPLY(
        SELECT *
            FROM StudentAge
            WHERE Student.Id = StudentAge.Student_Id
    )StudentAges

They all produced the same thing.

Image 2

http://www.sqlfiddle.com/#!3/517ad/2

Right Joins

Each person shakes hand if they know each other, embarrassment revealed for the right man.

Join: RIGHT JOIN
SQL
/*Join: RIGHT JOIN*/
/*does: Each person shakes hand if they know each other, embarrassment revealed for the right man*/
SELECT *
    FROM Student
    RIGHT JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
Join: RIGHT OUTER JOIN
SQL
/*Join: RIGHT OUTER JOIN*/
/*does: as RIGHT JOIN*/
SELECT *
    FROM Student
    RIGHT OUTER JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
OUTER APPLY
SQL
/*OUTER APPLY*/
/*does: as LEFT JOIN, but presented as RIGHT JOIN using (SELECT Students.*, StudentAge.**/        
SELECT Students.*, StudentAge.*
    FROM StudentAge
    OUTER APPLY(
        SELECT *
            FROM Student
            WHERE StudentAge.Student_Id = Student.Id
    )Students

They all produced the same thing.

Image 3

http://www.sqlfiddle.com/#!3/517ad/3

Full Joins

Each person shakes hand if they know each other, embarrassment revealed for both men.

Join: FULL JOIN
SQL
/*Join: FULL JOIN*/
/*does: Each person shakes hand if they know each other, embarrassment revealed for the both man*/
SELECT *
    FROM Student
    FULL JOIN StudentAge ON Student.Id = StudentAge.Student_Id;
Join: FULL OUTER JOIN
SQL
/*Join: FULL OUTER JOIN*/
/*does: as FULL JOIN*/
SELECT *
    FROM Student
    FULL OUTER JOIN StudentAge ON Student.Id = StudentAge.Student_Id;

They all produced the same thing.

Image 4

http://www.sqlfiddle.com/#!3/517ad/4

Cross Joins

Each one shakes hand with everyone.

Join: CROSS JOIN
SQL
/*Join: CROSS JOIN*/
/*does: Each one shakes hand with every one*/
SELECT *
    FROM Student
    CROSS JOIN StudentAge;
Join: TABLE
SQL
/*Join: TABLE*/
/*does: as CROSS JOIN*/
SELECT *
    FROM Student, StudentAge;
CROSS APPLY
SQL
/*CROSS APPLY*/
/*does: as CROSS JOIN*/
SELECT *
    FROM Student
    CROSS APPLY(
        SELECT *
            FROM StudentAge
    )StudentAges

They all produced the same thing.

Image 5

Image 6

http://www.sqlfiddle.com/#!3/517ad/5

Other Joins!!!

When working with joins, we may face a situation where we want to do few more things like:

  • LEFT JOIN EXCLUDING INNER JOIN
  • RIGHT JOIN EXCLUDING INNER JOIN
  • OUTER JOIN EXCLUDING INNER JOIN
  • Etc. or some real basics

Check it out at http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins, which is a great article by C.L. Moffatt.

Cross/Outer Apply !!!

The only intention to use Cross/ Outer Apply is performance issue. It performs faster than the normal joins. For more, take some overview from:

Limitations

Yes, there could be something which I misunderstood or presented. So if you find anything, just let me know.

License

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


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mou_kol5-Aug-22 23:02
Mou_kol5-Aug-22 23:02 
GeneralTaken a huge 'part' ;) Pin
Nitol Neophyte19-Apr-15 6:40
Nitol Neophyte19-Apr-15 6:40 
GeneralRe: Taken a huge 'part' ;) Pin
DiponRoy19-Apr-15 7:56
DiponRoy19-Apr-15 7:56 
GeneralRe: Taken a huge 'part' ;) Pin
Nitol Neophyte23-Apr-15 0:23
Nitol Neophyte23-Apr-15 0:23 
GeneralMessage Closed Pin
23-Apr-15 0:23
Nitol Neophyte23-Apr-15 0:23 
GeneralRe: Taken a huge 'part' ;) Pin
CHill6023-Apr-15 0:27
mveCHill6023-Apr-15 0:27 
GeneralMy vote of 5 Pin
Sibeesh KV19-Sep-14 19:44
professionalSibeesh KV19-Sep-14 19:44 
GeneralRe: My vote of 5 Pin
DiponRoy20-Sep-14 1:20
DiponRoy20-Sep-14 1:20 
GeneralRe: My vote of 5 Pin
Sibeesh KV20-Sep-14 3:56
professionalSibeesh KV20-Sep-14 3:56 
GeneralMy vote of 3 Pin
coded00720-Aug-14 2:53
professionalcoded00720-Aug-14 2:53 
GeneralRe: My vote of 3 Pin
DiponRoy20-Aug-14 3:57
DiponRoy20-Aug-14 3:57 
GeneralMy vote of 1 Pin
BufferUnderrun20-Aug-14 2:29
BufferUnderrun20-Aug-14 2:29 
GeneralRe: My vote of 1 Pin
DiponRoy20-Aug-14 4:03
DiponRoy20-Aug-14 4:03 
please explain it to me, "Why this post is completely useless ?", explain it to me, so that i can improve it.
I am waiting for your ans.
Suggestionspell check nitpicking Pin
afbach19-Aug-14 7:32
afbach19-Aug-14 7:32 
GeneralRe: spell check nitpicking Pin
DiponRoy19-Aug-14 8:58
DiponRoy19-Aug-14 8:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.