Click here to Skip to main content
15,896,154 members
Articles / Programming Languages / SQL
Tip/Trick

Right vs. Left Outer Join

Rate me:
Please Sign up or sign in to vote.
4.35/5 (11 votes)
11 Nov 2014CPOL1 min read 30.4K   12   8
The explanation of left and right outer join.

Introduction

JOIN clause is a basic construct in SQL used to combine rows from two or more tables. They are commonly used, but every time before writing a statement with join, many people start wondering what the result will be. The best way of understanding joins is to visualize them by using Venn diagrams.

Many beginners wonder why right joins are introduced when left exist. So, let's take a closer look at two types of joins: right and left, which seem to be the most interesting.

It's better to work on real data, so let's present two tables and fill them.

Image 1

LEFT OUTER JOIN

LEFT OUTER JOIN retrieves rows from TableA with matching records from TableB. If for a certain record from TableA (left), there are no matching records from TableB (right), the corresponding (right) columns contain nulls.

Image 2

SQL
Select *
FROM TableA
LEFT OUTER JOIN TableB
on tableA.name = tableB.name;

Image 3

RIGHT OUTER JOIN

RIGHT OUTER JOIN retrieves rows from TableB with matching records from TableA. This situation is the opposite of the previous one. Here, when for a certain record from TableB (right), there are no matching records from TableA (left), the corresponding (left) columns contain nulls.

Image 4

SQL
Select *
FROM tableA
RIGHT OUTER JOIN tableB
On tableA.name = tableB.name

Image 5

Of course, right outer join can be achieved by doing a left outer join with swapped tables. The question occurs: Why does right outer join exist when there is left outer join?

In SQLite database, there is no such thing as right and full outer join. They both can be emulated by left outer join.

The example of full outer join in sqlite:

SQL
select  *
from TableA left join TableB
on TableA.name = TableB.name 
union
select *
from TableB left join TableA
on TableB.name = TableA.name

And one last note. LEFT OUTER JOIN = LEFT JOIN and RIGHT OUTER JOIN = RIGHT JOIN. You can find a full review of SQL joins here.

License

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


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

Comments and Discussions

 
QuestionI prefer this in simple. Pin
Cheung Tat Ming15-Nov-14 21:15
Cheung Tat Ming15-Nov-14 21:15 
QuestionPlaying around Pin
KP Lee13-Nov-14 1:29
KP Lee13-Nov-14 1:29 
Note that using left joins on reversed tables won't work selecting * if both tables aren't syncronous. (IE same fields/types, sequence)
I think ansi null settings could affect the joining results on null joins. Note that the full join has the same results in a different order because order by wasn't used.
Personally I like the power and easy scripting full outer join provides.
Here are some play scripts to see what I'm talking about:
Two tables selected separately so you can see union wouldn't work(6&7 rows) then joined with left and full (Same 8 rows, note the column order differences)

declare @tbl1 table(id int identity primary key, tb1Dta varchar(30) null, tb1Dta2 int, vals varchar(10))
declare @tbl2 table(vals varchar(10), tb2Dta int null, tb2Dta2 varchar(30))
insert @tbl1 values('dta1',5,'vdta'),('dta3',75,'vdta3'),(null,15,'vdta'),('dta5112345',53,null)
insert @tbl2 values('vdta',33,'dta51'),('dta2',22,'vdta3'),(null,15,'vdta'),('vdta',53,'dta987654321051'),(null,15,'vdta')
select * from @tbl1 a left join @tbl2 b on a.vals=b.vals
--union here will blow up
select * from @tbl2 b left join @tbl1 a on a.vals=b.vals
--union technique that will work
select * from @tbl1 a left join @tbl2 b on a.vals=b.vals
union 
select a.*, b.* from @tbl2 b left join @tbl1 a on a.vals=b.vals
-- full join same results but different column order because I kept the 2nd reversed table order.
select * from @tbl2 b full join @tbl1 a on a.vals=b.vals

GeneralMy vote of 5 Pin
Mahsa Hassankashi12-Nov-14 1:48
Mahsa Hassankashi12-Nov-14 1:48 
QuestionMessage Closed Pin
11-Nov-14 22:54
Member 1121439311-Nov-14 22:54 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Nov-14 22:41
Humayun Kabir Mamun11-Nov-14 22:41 
GeneralMy vote of 4 Pin
Tomas Takac11-Nov-14 22:04
Tomas Takac11-Nov-14 22:04 
GeneralRe: My vote of 4 Pin
KP Lee13-Nov-14 1:42
KP Lee13-Nov-14 1:42 
GeneralRe: My vote of 4 Pin
Tomas Takac13-Nov-14 3:01
Tomas Takac13-Nov-14 3:01 

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.