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 1 thread Add post
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);