Click here to Skip to main content
Click here to Skip to main content

Visual Representation of SQL Joins

By , 3 Feb 2009
 

Introduction

This is just a simple article visually explaining SQL JOINs.

Background

I'm a pretty visual person. Things seem to make more sense as a picture. I looked all over the Internet for a good graphical representation of SQL JOINs, but I couldn't find any to my liking. Some had good diagrams but lacked completeness (they didn't have all the possible JOINs), and some were just plain terrible. So, I decided to create my own and write an article about it.

Using the code

I am going to discuss seven different ways you can return data from two relational tables. I will be excluding cross Joins and self referencing Joins. The seven Joins I will discuss are shown below:

  1. INNER JOIN
  2. LEFT JOIN
  3. RIGHT JOIN
  4. OUTER JOIN
  5. LEFT JOIN EXCLUDING INNER JOIN
  6. RIGHT JOIN EXCLUDING INNER JOIN
  7. OUTER JOIN EXCLUDING INNER JOIN

For the sake of this article, I'll refer to 5, 6, and 7 as LEFT EXCLUDING JOIN, RIGHT EXCLUDING JOIN, and OUTER EXCLUDING JOIN, respectively. Some may argue that 5, 6, and 7 are not really joining the two tables, but for simplicity, I will still refer to these as Joins because you use a SQL Join in each of these queries (but exclude some records with a WHERE clause).

Inner JOIN

INNER_JOIN.png

This is the simplest, most understood Join and is the most common. This query will return all of the records in the left table (table A) that have a matching record in the right table (table B). This Join is written as follows:

SELECT <select_list> 
FROM Table_A A
INNER JOIN Table_B B
ON A.Key = B.Key

Left JOIN

LEFT_JOIN.png

This query will return all of the records in the left table (table A) regardless if any of those records have a match in the right table (table B). It will also return any matching records from the right table. This Join is written as follows:

SELECT <select_list>
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key

Right JOIN

RIGHT_JOIN.png

This query will return all of the records in the right table (table B) regardless if any of those records have a match in the left table (table A). It will also return any matching records from the left table. This Join is written as follows:

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key

Outer JOIN

FULL_OUTER_JOIN.png

This Join can also be referred to as a FULL OUTER JOIN or a FULL JOIN. This query will return all of the records from both tables, joining records from the left table (table A) that match records from the right table (table B). This Join is written as follows:

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key

Left Excluding JOIN

LEFT_EXCLUDING_JOIN.png

This query will return all of the records in the left table (table A) that do not match any records in the right table (table B). This Join is written as follows:

SELECT <select_list> 
FROM Table_A A
LEFT JOIN Table_B B
ON A.Key = B.Key
WHERE B.Key IS NULL

Right Excluding JOIN

RIGHT_EXCLUDING_JOIN.png

This query will return all of the records in the right table (table B) that do not match any records in the left table (table A). This Join is written as follows:

SELECT <select_list>
FROM Table_A A
RIGHT JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL

Outer Excluding JOIN

OUTER_EXCLUDING_JOIN.png

This query will return all of the records in the left table (table A) and all of the records in the right table (table B) that do not match. I have yet to have a need for using this type of Join, but all of the others, I use quite frequently. This Join is written as follows:

SELECT <select_list>
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.Key = B.Key
WHERE A.Key IS NULL OR B.Key IS NULL

Examples

Suppose we have two tables, Table_A and Table_B. The data in these tables are shown below:

TABLE_A
  PK Value
---- ----------
   1 FOX
   2 COP
   3 TAXI
   6 WASHINGTON
   7 DELL
   5 ARIZONA
   4 LINCOLN
  10 LUCENT

TABLE_B
  PK Value
---- ----------
   1 TROT
   2 CAR
   3 CAB
   6 MONUMENT
   7 PC
   8 MICROSOFT
   9 APPLE
  11 SCOTCH

The results of the seven Joins are shown below:

-- INNER JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
       B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
INNER JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7

(5 row(s) affected)
-- LEFT JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   4 LINCOLN    NULL       NULL
   5 ARIZONA    NULL       NULL
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
  10 LUCENT     NULL       NULL

(8 row(s) affected)
-- RIGHT JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11

(8 row(s) affected)
-- OUTER JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   1 FOX        TROT          1
   2 COP        CAR           2
   3 TAXI       CAB           3
   6 WASHINGTON MONUMENT      6
   7 DELL       PC            7
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
   5 ARIZONA    NULL       NULL
   4 LINCOLN    NULL       NULL
  10 LUCENT     NULL       NULL

(11 row(s) affected)
-- LEFT EXCLUDING JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
LEFT JOIN Table_B B
ON A.PK = B.PK
WHERE B.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
   4 LINCOLN    NULL       NULL
   5 ARIZONA    NULL       NULL
  10 LUCENT     NULL       NULL
(3 row(s) affected)
-- RIGHT EXCLUDING JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
RIGHT JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11

(3 row(s) affected)
-- OUTER EXCLUDING JOIN
SELECT A.PK AS A_PK, A.Value AS A_Value,
B.Value AS B_Value, B.PK AS B_PK
FROM Table_A A
FULL OUTER JOIN Table_B B
ON A.PK = B.PK
WHERE A.PK IS NULL
OR B.PK IS NULL

A_PK A_Value    B_Value    B_PK
---- ---------- ---------- ----
NULL NULL       MICROSOFT     8
NULL NULL       APPLE         9
NULL NULL       SCOTCH       11
   5 ARIZONA    NULL       NULL
   4 LINCOLN    NULL       NULL
  10 LUCENT     NULL       NULL

(6 row(s) affected)

Note on the OUTER JOIN that the inner joined records are returned first, followed by the right joined records, and then finally the left joined records (at least, that's how my Microsoft SQL Server did it; this, of course, is without using any ORDER BY statement).

You can visit the Wikipedia article for more info here (however, the entry is not graphical).

I've also created a cheat sheet that you can print out if needed. If you right click on the image below and select "Save Target As...", you will download the full size image.

History

  • Initial release -- 02/03/2009.
  • Version 1.0 -- 02/04/2009 -- Fixed cheat sheet and minor typos.

License

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

About the Author

C.L. Moffatt
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThe best introduction to joins Pinmembergdhgasyue16 Nov '11 - 20:50 
GeneralRe: The best introduction to joins PinmemberC.L. Moffatt30 Nov '11 - 9:09 
GeneralMy vote of 5 Pinmemberoperations12 Nov '11 - 3:02 
GeneralRe: My vote of 5 PinmemberC.L. Moffatt30 Nov '11 - 9:09 
QuestionMy vote of 5 PinmemberDrazen Pupovac31 Oct '11 - 0:34 
GeneralMy vote of 5 PinmemberA-Lexo28 Sep '11 - 23:23 
Very informative graphs.
SuggestionFew more things to add Pinmembershantanufrom438719 Jul '11 - 21:26 
GeneralRe: Few more things to add PinmemberC.L. Moffatt20 Jul '11 - 9:21 
GeneralMy vote of 5 PinmemberReiss12 Jul '11 - 10:30 
GeneralMy vote of 5 Pinmemberead mahmoud19 Apr '11 - 2:58 
GeneralMy vote of 5 PinmemberDaveAuld17 Apr '11 - 22:39 
GeneralMy vote of 5 Pinmemberdcialdella16 Feb '11 - 4:59 
GeneralMy vote of 5 PinmemberVijay Chandra Sekhar Parepalli12 Feb '11 - 15:58 
GeneralMy vote of 5 PinmemberWayne Clarke1 Feb '11 - 5:08 
GeneralMy vote of 5 PinmemberSteve Maier24 Jan '11 - 9:26 
Generalmy vote of 5 PinmemberYusuf8 Jan '11 - 16:45 
GeneralJOINS Pinmemberthinkindia10 Dec '10 - 1:55 
GeneralMy vote of 5 Pinmemberlinuxjr23 Nov '10 - 1:53 
GeneralThis assumes no NULL for keys PinmemberLenzM3 Nov '10 - 18:34 
GeneralRe: This assumes no NULL for keys PinmemberC.L. Moffatt28 Jan '11 - 5:46 
GeneralMy vote of 5 Pinmembersomnath _chowdhury29 Oct '10 - 0:54 
GeneralMy vote of 5 PinmemberChristophe30 Sep '10 - 2:53 
GeneralMy vote of 5 Pinmembermaster vicky29 Sep '10 - 1:20 
GeneralMy vote of 5 PinmemberPranay Rana22 Jul '10 - 1:44 
GeneralMy vote of 5 Pinmemberza_10041719 Jul '10 - 0:55 
GeneralMy vote of 5 PinmemberDarkRisingForce14 Jul '10 - 20:41 
GeneralExcellent. Pinmemberdigital man11 Mar '10 - 23:23 
GeneralGreat Article!! PinmemberKhushi_G18 Feb '10 - 10:14 
Generalgot my 5 PinmemberOmar Gamil18 Feb '10 - 3:37 
Generalgreat article PinmembercrudeCodeYogi24 Jan '10 - 7:41 
GeneralExcillent PingroupMd. Marufuzzaman30 Dec '09 - 1:59 
Questionhow to add new table (after join) to a datagridview Pinmemberanahita2213 Aug '09 - 21:15 
AnswerRe: how to add new table (after join) to a datagridview PinmemberC.L. Moffatt17 Aug '09 - 10:58 
GeneralLeft outer join and right outer join Pinmemberudomnoke17 Jul '09 - 15:20 
GeneralRe: Left outer join and right outer join PinmemberC.L. Moffatt17 Aug '09 - 10:59 
GeneralRe: Left outer join and right outer join PinmemberUnruled Boy27 Jul '11 - 1:39 
GeneralRe: Left outer join and right outer join PinmemberC.L. Moffatt27 Jul '11 - 11:52 
GeneralRe: Left outer join and right outer join PinmemberUnruled Boy27 Jul '11 - 16:54 
GeneralReally Good! PinmemberMANISH RASTOGI25 Jun '09 - 21:03 
QuestionArticle Translation PinmemberRodrigo Salvaterra13 May '09 - 7:59 
AnswerRe: Article Translation PinmemberC.L. Moffatt13 May '09 - 8:35 
GeneralThank you, Great job Pinmembernishchal245 May '09 - 22:45 
QuestionImitation? PinmemberHoyaSaxa9314 Apr '09 - 4:01 
AnswerRe: Imitation? PinmemberC.L. Moffatt21 Apr '09 - 13:40 
GeneralGood work PinmemberDonsw24 Feb '09 - 7:30 
GeneralGreat Stuff [modified] Pinmemberweaponx2007a17 Feb '09 - 14:35 
GeneralGreat work Pinmembermaga8217 Feb '09 - 6:39 
GeneralJust one thing to perhaps alter PinmemberMark Greenwood12 Feb '09 - 12:05 
GeneralRe: Just one thing to perhaps alter PinmemberC.L. Moffatt12 Feb '09 - 17:40 
GeneralRe: Just one thing to perhaps alter Pinmemberd_camillo13 Feb '09 - 12:14 

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Feb 2009
Article Copyright 2009 by C.L. Moffatt
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid