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.

You can increase the size of the video:

Full screen mode for YouTube

You can view the video in full screen mode as shown on the left, using the icon at the bottom right of the frame.

You can also increase the quality of the video:

Changing resolution

You can improve the resolution of the video using another icon at the bottom right of the frame. This will slow down the connection speed, but increase the display and sound quality. This icon only becomes visible when you start playing the video.

Finally, if nothing happens when you play the video, check that you're not using IE in compatibility view.

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