Writing inner joins and outer joins in SQL
Part one of a six-part series of blogs

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!

  1. Inner Joins, Outer Joins and Shake-it-all-about Joins (this blog)
  2. Creating Joins using your Mouse - the Query Editor
  3. Writing Inner Joins in SQL
  4. Full, Left and Right Outer Joins in SQL
  5. Cross Joins using SQL
  6. 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).

Two tables joined together

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:

Table of authors The table of 9 books
The table of 4 authors The table of 9 books

You will notice that there are mismatches between the tables:

Authors table highlighting author with no books Highlighting books with no authors
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:

Venn diagram of joins

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




USE Bookshop



-- create table of books



[BookName] [varchar](100) NOT NULL,

[AuthorId] [int] NULL,)



-- add some books into table


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)



-- create table of authors

CREATE TABLE [tblAuthor](


[FirstName] [varchar](50) NOT NULL,

[LastName] [varchar](50) NOT NULL




-- add authors


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')


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.

This blog has 0 threads Add post