Selecting data in SQL videos | SQL Queries Part 6 - Using Joins in Queries

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.

There are no files which go with this video.

Making a  video bigger

You can increase the size of your video to make it fill the screen like this:

View full screen

Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown at its bottom right-hand corner.

 

When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.

Improving the quality of a video

To improve the quality of a video, first click on the Settings icon:

Settings icon

Make sure you're playing your video so that the icons shown appear, then click on this gear icon at the bottom right-hand corner.

 

Choose to change the video quality:

Video quality

Click on Quality as shown to bring up the submenu.

 

The higher the number you choose, the better will be your video quality (but the slower the connection speed):

Connection speed

Don't choose the HD option unless you have a fast enough connection speed 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.

This page has 2 threads Add post
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

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.

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?

Andy B  
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

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?

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.

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