How to Master SQL Joins - Visual Guide to SQL Joins with Examples

This visual guide to SQL joins should help you determine which one of the various SQL join types to use for the query you need to run.

By Tim Trott | SQL and mySql Tutorials | February 24, 2009

When combining rows from multiple tables in one query, you may need to use the JOIN command. There are a few different types of joins, and the following should help explain the differences between them. The syntax will vary depending on which database type you are using. For more details, see the documentation for your database, linked below.

In this example, I have a photo gallery database with two tables. One table is for Galleries and the other for Photos.

Gallery Table

id title
1 Landscape
2 Architecture
3 Macro
4 Wildlife
5 Portrait
6 Black and White

Photo Table

id title gallery_id
1 hills.jpg 1
2 rivers.jpg 1
3 mountains.jpg 1
4 cityscape.jpg 2
5 tiger.jpg 4
6 elephant.jpg 4
7 queen.jpg 5
8 space.jpg 99

Notice that in this dataset that there are no photos in the macro or black and white galleries, and that space.jpg is assigned to a gallery that doesn't exist in the Gallery table.

Let's join these tables by the name field in a few different ways and see if we can get a conceptual match to those nifty Venn diagrams.

SQL Join - Inner Join

The inner join is the default used when you don't specify the type of Join. When you do an inner join of two tables, it returns results which are matched in both tables.

sql
SELECT * FROM photos
INNER JOIN galleries
ON photos.gallery = galleries.id;

SQL Inner Join
SQL Inner Join
id name gallery id name
1 hills.jpg 1 1 Landscape
2 rivers.jpg 1 1 Landscape
3 mountains.jpg 1 1 Landscape
4 cityscape.jpg 2 2 Archetecture
5 tiger.jpg 4 4 Wildlife
6 elephant.jpg 4 4 Wildlife
7 queen.jpg 5 5 Portrait

SQL Join - Full Outer Join

Full outer join produces the set of all records in Table A and Table B, with matching records from both sides where available. If there is no match, the missing side will contain null.

sql
SELECT * FROM photos
FULL OUTER JOIN galleries
ON photos.id = galleries.id

SQL Full Outer Join
SQL Full Outer Join
id gallery id name name
1 1 1 Landscape hills.jpg
2 1 1 Landscape rivers.jpg
3 1 1 Landscape mountains.jpg
4 2 2 Architecture cityscape.jpg
5 4 4 Wildlife tiger.jpg
6 4 4 Wildlife elephant.jpg
7 5 5 Portrait queen.jpg
8 99 NULL NULL space.jpg
NULL NULL 3 Macro NULL
NULL NULL 6 Black and White NULL

SQL Join - Left Outer Join

Left outer join produces a complete set of records from Table A, with the matching records (where available) in Table B. If there is no match, the right side will contain null.

sql
SELECT * FROM photos
LEFT OUTER JOIN galleries
ON photos.gallery = galleries.id
WHERE galleries.id IS null

SQL Left Outer Join
SQL Left Outer Join
gallery id name name id
99 NULL NULL space.jpg 8

To produce the set of records only in Table A, but not in Table B, we perform the same left outer join, then exclude the records we don't want from the right side via a where clause.

sql
SELECT * FROM photos
FULL OUTER JOIN galleries
ON photos.gallery = galleries.id
WHERE galleries.id IS null

SQL Left Outer Join
SQL Left Outer Join
gallery id name name id
1 1 Landscape hills.jpg
1 1 Landscape river.jpg 2
1 1 Landscape mountains.jpg 3
2 2 Architecture cityscape.jpg 4
4 4 Wildlife tiger.jpg 5
4 4 Wildlife elephant.jpg 6
5 5 Portrait queen.jpg 7
99 NULL NULL space.jpg 8

To produce the set of records unique to Table A and Table B, we perform the same full outer join, then exclude the records we don't want from both sides via a where clause.

sql
SELECT * FROM photos
FULL OUTER JOIN galleries
ON photos.id = galleries.id
WHERE photos.gallery IS null
OR galleries.id IS null

SQL Joins
SQL Joins
gallery id name name id
1 1 Landscape hills.jpg
1 1 Landscape river.jpg 2
1 1 Landscape mountains.jpg 3
2 2 Architecture cityscape.jpg 4
4 4 Wildlife tiger.jpg 5
4 4 Wildlife elephant.jpg 6
5 5 Portrait queen.jpg 7
99 NULL NULL space.jpg 8
NULL 3 Macro NULL NULL
NULL 6 Black and White NULL NULL
Was this article helpful to you?
 

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

We respect your privacy, and will not make your email public. Learn how your comment data is processed.