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
The previous part of this tutorial showed all of the matching rows in two tables - but what about if you want to show unmatching ones too?
An author with no books, and ... | ... books with no valid author |
Here's how to create a query to show all of the authors (as an example), along with any books that the author has written:
The left column comes from table tblAuthor; the right one from table tblBook. Notice that Oliver Owl has NULL next to him, as he hasn't got any corresponding books in the tblBook table.
For an inner join, it doesn't make any difference which way round you create a link (linking authors to books is just the same as linking books to authors). For an outer join, however, the order does matter.
In our example, we want to show all of the authors (and alongside that, any books each author has written), so the tblAuthor table is the main focus of our query and should be listed first:
A sensible layout for the two tables.
We'll therefore create a left outer join from the tblAuthor table to the tblBook table.
The term LEFT OUTER JOIN is a bit silly, as you're writing SQL into a text editor, and the concepts of left and right aren't defined there!
Having decided on an order for our tables, it's time to write the join. The syntax is nearly identical to that for an inner join:
SELECT
-- show name of author ...
a.FirstName + ' ' + a.LastName AS 'Author name',
-- and any book they've written
b.BookName
FROM
tblAuthor AS a
LEFT OUTER JOIN tblBook AS b
ON a.AuthorId=b.AuthorId
ORDER BY
'Author Name'
This is what this will produce:
The output from our query, showing each author and the books they've written.
If you wanted, you could change the order of the tables for this query and rewrite it to do exactly the same thing, but as a right outer join:
SELECT
-- show name of author ...
a.FirstName + ' ' + a.LastName AS 'Author name',
-- and any book they've written
b.BookName
FROM
tblBook AS b
RIGHT OUTER JOIN tblAuthor AS a
ON a.AuthorId=b.AuthorId
ORDER BY
'Author Name'
If you want to show all of the records in one table which have no corresponding entries in another, you can do so by writing an outer join query with a criteria to pick out all of the records in one table where the corresponding records in the other table don't exist:
-- show all the authors who haven't written books
SELECT
a.FirstName + ' ' + a.LastName AS 'Author name'
FROM
tblAuthor AS a
LEFT OUTER JOIN tblBook AS b
ON a.AuthorId=b.AuthorId
WHERE
b.BookId is null
ORDER BY
'Author Name'
Here's what this query would produce:
The only author who doesn't have any corresponding books.
Alternatively, you could do this the other way round, to show a list of the books whose AuthorId columns don't have corresponding values in the tblAuthor table:
-- show all the books for which we can't find authors
SELECT
b.BookName
FROM
tblBook AS b
LEFT OUTER JOIN tblAuthor AS a
ON a.AuthorId=b.AuthorId
WHERE
a.AuthorId is null
ORDER BY
BookName
Here are the two records this query would show:
The two books for which we can't find a matching author in the authors table.
A left or right outer join will focus on showing all of the records from one table (with corresponding rows from another table where they exist). A full outer join will show all of the records from all of the tables:
-- show every book and every author
SELECT
a.FirstName + ' ' + a.LastName
AS 'Author name',
b.BookName
FROM
tblAuthor AS a
FULL OUTER JOIN tblBook AS b
ON a.AuthorId=b.AuthorId
ORDER BY
'Author Name',
b.BookName
Here are the rows that this query would return:
The query will show all authors (including those who don't have corresponding books) and all books (including those with no corresponding authors).
It's hard to see the point of a full outer join. I've certainly never used one in anger!
You can mix outer and inner joins as you like. The following query would show a list of all of the film directors from a table called tblDirector, along with the films, characters and actors for each director:
SELECT
d.DirectorName,
f.FilmName,
c.CastCharacterName,
a.ActorName
FROM
tblFilm AS f
INNER JOIN tblCast AS c
ON f.FilmID = c.CastFilmID
INNER JOIN tblActor AS a
ON c.CastActorID = a.ActorID
RIGHT OUTER JOIN tblDirector As d
ON f.FilmDirectorID = d.DirectorID
This query mixes a right outer join with two inner joins.
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.