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
550 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 ...
Posted by Andrew Gould on 23 May 2012
Learn how to use joins in Microsoft SQL Server queries. This video teaches you how to include multiple tables in a query using INNER joins, how to find unmatched records using LEFT and RIGHT OUTER joins, and how to use FULL OUTER joins.
See our full range of SQL training resources, or test your knowledge of SQL with one of our SQL skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
Create Movies Database with Genre Table.sql | SQL query | |
GenerateMoviesDatabase.sql | SQL query | |
GenerateUpdatedMoviesDatabase.sql | SQL query |
Click to download a zipped copy of the above files.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | duggie |
When: | 07 Oct 20 at 13:26 |
In the video, around the 9min 48 sec mark, you state you wanted to see everything in the director's table and to do that, you use a LEFT OUTER join.
Imagine if the actor's table didn't exist. If you wanted to see everything in the director's table, would you simply write:
SELECT * FROM tbldirector
If so, even if the actor's table is "brought into the equation", to see everything in the director's table, wouldn't my query work?
On a separate note, if you do have both tables, which part of the venn diagram would be highlighted for this query:
SELECT * FROM tbldirector
From: | Andy B |
When: | 07 Oct 20 at 13:45 |
I'm answering on behalf of Andrew, as he's busy training today.
The following two commands are identical:
SELECT * FROM tblDirector
SELECT tblDirector.* FROM tblDirector LEFT OUTER JOIN tblFilm ON tblDirector.DirectorId = tblFilm.DirectorId
The first command will list out all the rows and columns in the director table; the second will do the same thing. The only reason to use the second syntax is if you want either to list out columns from the films table, or use some of them for filtering or grouping.
If you run the first command, I'm not even sure there IS a Venn diagram (and if it is, it's just a circle). What this will list out is all directors, whether or not they've made films.
From: | duggie |
When: | 07 Oct 20 at 14:48 |
Thanks for your swift reply.
However, I tried typing your SQL command:
SELECT tblDirector.* FROM tblDirector LEFT OUTER JOIN tblFilm ON tblDirector.DirectorId = tblFilm.DirectorId
but got an error.
Is it my mistake?
From: | Andy B |
When: | 07 Oct 20 at 15:34 |
OK, I used the old table names. Try:
SELECT Director.* FROM Director LEFT OUTER JOIN Film ON Director.DirectorId = Film.DirectorId
From: | duggie |
When: | 21 Oct 17 at 12:23 |
I find using joins difficult to understand.
In the video, you have:
select
d.directorid,
d.directorname,
f.filmname,
f.filmdirectorid
from
tbldirector as d
inner join tblFilm as f
on d.directorid=f.FilmDirectorID
I prefer the following:
select
d.directorid,
d.directorname,
f.filmname,
f.filmdirectorid
from tbldirector as d
, tblFilm as f
WHERE d.directorid=f.FilmDirectorID
However, I can't seem to do the same for a LEFT JOIN.
I tried to replicate this query
select
d.directorid,
d.directorname,
f.filmname,
f.filmdirectorid
from
tbldirector as d
left join tblFilm as f
on d.directorid=f.FilmDirectorID
by writing this but it returned more results (though I can't be sure if all the records from the query using LEFT JOIN is contained in this query):
select
d.directorid,
d.directorname,
f.filmname,
f.filmdirectorid
from tbldirector as d
, tblFilm as f
WHERE d.directorid=f.FilmDirectorID OR f.FilmDirectorID IS NULL
What is wrong?
From: | Andy B |
When: | 21 Oct 17 at 12:29 |
I think the answer to your question is that our way is better! I can't think of a way to use your method to solve the second problem. Although conceptually your method is perhaps easier to understand, I think I would take the time to learn how to write inner and outer joins. Not only are they more flexible (as you have seen), but the vast majority of examples on the Internet use joins.
From: | duggie |
When: | 22 Oct 17 at 22:07 |
Andy,
I agree with you that in the long run, it's more beneficial to get accustomed to using JOINS
Out of curiosity for fellow readers, someone on another forum replied with this response to NOT using JOINS:
SELECT d.directorid, d.directorname, f.filmname, f.filmdirectorid
FROM tbldirector AS d, tblFilm AS f
WHERE d.directorid = f.FilmDirectorID
UNION
SELECT d.directorid, d.directorname, null filmname, null filmdirectorid
FROM tbldirector d
WHERE NOT EXISTS (SELECT 1 FROM tblFilm AS f WHERE d.directorid=f.FilmDirectorID);
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.