BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
An in-depth SQL tutorial on how to create inner joins, left outer joins, right outer joins, full outer joins, cross joins and self-joins!
- Inner Joins, Outer Joins and Shake-it-all-about Joins (this blog)
- Creating Joins using your Mouse - the Query Editor
- Writing Inner Joins in SQL
- Full, Left and Right Outer Joins in SQL
- Cross Joins using SQL
- Self-Joins in SQL (joining a table to itself)
This online training on joins is part of a longer tutorial on the whole of SQL. Naturally, Wise Owl also run a range of SQL training courses for individualas and (above all) for businesses.
Posted by Andy Brown on 30 November 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Inner Joins, Outer Joins and Shake-it-all-about Joins
This blog aims to show - in an easy-to-understand way - just about everything useful there is to know about joining tables in T-SQL (the dialect of SQL used by SQL Server).

You can represent a join between tables in a database diagram - but how can you do this in SQL?
Our Example Database
Throughout the whole of this blog, I'll use the simple database shown above. Here are the two tables in this database:
![]() |
![]() |
The table of 4 authors | The table of 9 books |
You will notice that there are mismatches between the tables:
![]() |
![]() |
One author has no books | Two books have no authors |
A Summary of the Joins Available
Given the example above, you can use the following types of join (explained in more detail throughout the rest of this blog):
Type of join | What it would show |
---|---|
Inner | The 7 books and 3 authors which match up to each other. |
Left outer join | The 4 authors (together with any books they've written). |
Right outer join | The 9 books (together with their authors, where found). |
Cross join | The 36 possible combinations of author and book. |
As we'll discover, the concepts of right and left outer join are interchangeable, but I've assumed here the tables are joined from left to right.
A Venn Diagram View of the Tables
For those who remember their Venn diagrams from schoolday maths (or math, if you're reading this in the States), here's a summary of the number of records in each table:

There are 4 authors, but one of them hasn't written any books.
Generating the Sample Database
If you want to follow through the examples in the rest of this blog, run the following script in SQL Server Management Studio to generate the Bookshop database shown above:
-- create a new database
CREATE DATABASE Bookshop
GO
USE Bookshop
GO
-- create table of books
CREATE TABLE [tblBook](
[BookId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[BookName] [varchar](100) NOT NULL,
[AuthorId] [int] NULL,)
GO
-- add some books into table
SET IDENTITY_INSERT [tblBook] ON
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (1, N'The Day of the Triffids', 2)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (2, N'Girl with Dragon Tattoo', 1)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (3, N'The Chrysalids', 2)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (4, N'The Kraken wakes', 2)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (5, N'The girl who Played with fire', 1)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (6, N'Emma', 4)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (7, N'Pride and Prejudice', 4)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (8, N'The Hobbit', 5)
INSERT [tblBook] ([BookId], [BookName], [AuthorId]) VALUES (9, N'The Life of Pi', 6)
SET IDENTITY_INSERT [tblBook] OFF
-- create table of authors
CREATE TABLE [tblAuthor](
[AuthorId] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[LastName] [varchar](50) NOT NULL
)
GO
-- add authors
SET IDENTITY_INSERT [tblAuthor] ON
INSERT [tblAuthor] ([AuthorId], [FirstName], [LastName]) VALUES (4, N'Jane', N'Austen')
INSERT [tblAuthor] ([AuthorId], [FirstName], [LastName]) VALUES (2, N'John', N'Wyndham')
INSERT [tblAuthor] ([AuthorId], [FirstName], [LastName]) VALUES (3, N'Oliver', N'Owl')
INSERT [tblAuthor] ([AuthorId], [FirstName], [LastName]) VALUES (1, N'Stieg', N'Larsson')
SET IDENTITY_INSERT [tblAuthor] OFF
Before we start looking at the nitty-gritty of inner and outer join syntax, let's look first at a way to avoid writing any SQL altogether.
- Inner Joins, Outer Joins and Shake-it-all-about Joins (this blog)
- Creating Joins using your Mouse - the Query Editor
- Writing Inner Joins in SQL
- Full, Left and Right Outer Joins in SQL
- Cross Joins using SQL
- Self-Joins in SQL (joining a table to itself)