Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 10th, 2009, 11:41 PM
Authorized User
 
Join Date: Sep 2008
Location: , , Singapore.
Posts: 87
Thanks: 1
Thanked 0 Times in 0 Posts
Default Sql joins

Hi,

is it possible to produce the same result using left outer join and right outer join.
if so can anyone explain me


Thanks in Advance
Raj
Reply With Quote
  #2 (permalink)  
Old February 11th, 2009, 08:50 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

It is possible if the two tables are in a one-to-one correspondence with respect to the JOIN condition.
__________________
-- Jeff
Reply With Quote
  #3 (permalink)  
Old February 11th, 2009, 05:00 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Short answer: Yes.

Example:
Code:
SELECT C.catName, S.subcatName
FROM Categories AS C LEFT JOIN SubCategories AS S
ON C.id = S.id
ORDER BY C.catName, S.subcatName
is 100% equivalent to doing
Code:
SELECT C.catName, S.subcatName
FROM SubCategories AS S RIGHT JOIN Categories AS C 
ON C.id = S.id
ORDER BY C.catName, S.subcatName
I'm not sure if that's what you were asking or if Jeff's answer--which basically says that the two are the same when an INNER JOIN would work as well--was what you meant.

Last edited by Old Pedant; February 11th, 2009 at 08:46 PM..
Reply With Quote
  #4 (permalink)  
Old February 11th, 2009, 06:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Old Pedant View Post
I'm not sure if that's what you were asking or if Jeff's answer--which basically says that the two are the same when an INNER JOIN would work as well--was what you meant.
Without JOIN conditions on your query, you are saying that constructing a cartesian product between two tables is commutative: A X B is the same as B X A.

Which of course it is.

Adding JOIN conditions to the cartesian product reduces it to an INNER JOIN.

So we're both right
__________________
-- Jeff
Reply With Quote
  #5 (permalink)  
Old February 11th, 2009, 08:47 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Sorry. The omission of the conditions was just an oversight. I went back and edited my post to add them in.

And I'm not clear why you say that adding join conditions reduces an OUTER join to the equivalent of an INNER JOIN. Ehhh...you couldn't have meant that. I'm misreading you.

As for "both right": Hell, I don't know if either of us is right. I still don't know exactly what the question was supposed to be asking.

Last edited by Old Pedant; February 11th, 2009 at 08:50 PM..
Reply With Quote
  #6 (permalink)  
Old February 11th, 2009, 10:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Hudson, MA, USA.
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by Old Pedant View Post
And I'm not clear why you say that adding join conditions reduces an OUTER join to the equivalent of an INNER JOIN. Ehhh...you couldn't have meant that. I'm misreading you..
Adding conditions to a cartesian product (not an OUTER JOIN) reduces it to an INNER JOIN. That is, in fact, how you 'do' an INNER JOIN: construct the cartesian product of the two tables, then select those rows where the JOIN condition is satisfied. I mean, that's how the math works; no query processor in it's right mind actually does it that way.

You are of course correct that A LEFT JOIN B ON ... is the equivalent of B RIGHT JOIN A ON...

Quote:
As for "both right": Hell, I don't know if either of us is right. I still don't know exactly what the question was supposed to be asking
I agree we really don't know what the OP was really getting at - we'll have to see if he shows back up to clarify...(or tells us which test question it was )
__________________
-- Jeff
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL - Help with Joins oldmuttonhead Classic ASP Databases 0 January 21st, 2006 05:02 PM
SQL Joins in XPath arcuza XSLT 0 March 16th, 2005 01:05 PM
Union joins in SQL sellis Access 3 April 7th, 2004 12:25 AM
SQL Table Joins Dredd Classic ASP Databases 2 June 3rd, 2003 06:24 PM



All times are GMT -4. The time now is 05:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.