Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
This tutorial 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?
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 |
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.
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.
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.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.