Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple Outer Join question

Author  Topic 

nmehta6
Starting Member

2 Posts

Posted - 2007-05-31 : 13:54:44
Ok here is the situation. I have 2 tables.

Movies
MovieID Title Year
21 A Beautiful Mind 2002
22 Forrest Gump 1994
23 The English Patient 1999


Actors
ActorID MovieID Name
1 22 Tom Hanks
2 21 Russell Crowe
3 23 Ralph Fiennes
4 NULL Nachiket Mehta

Here is the SQL Query.

SELECT ActorID, Name, Title, Year FROM Actors LEFT OUTER JOIN Movies ON Actors.MovieID = Movies.MovieID

Now, I only want to show movies made in 1990's and display all 4 actors. If I put

WHERE Year < 2000

it won't show the fourth actor because he doesn't have any movies. I need to show all 4 actors here and NULL for movies if they don't have any.

Hope this makes sense. Thank you.



Nachiket

sshelper
Posting Yak Master

216 Posts

Posted - 2007-05-31 : 14:04:42
Put your condition in the JOIN clause instead of the WHERE clause:

SELECT ActorID, Name, Title, Year FROM Actors LEFT OUTER JOIN Movies ON Actors.MovieID = Movies.MovieID AND Movies.Year < 2000


SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

nmehta6
Starting Member

2 Posts

Posted - 2007-05-31 : 14:31:18
Thanks. Works.

Nachiket
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-05-31 : 14:42:25
see also: http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/60205.aspx

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page
   

- Advertisement -