November 13, 2007 at 12:38 am
Hi
Below query is returning all rows of left table and matching rows of right table:
Select b.Col1, b.Col2, a.Col1
from Tab1 a join Tab2 b
on a.Col2 = b.Col2
As per my understanding, it must do a ineer join and should return only the rows where Col2 value are common in both tables. But it is behaving in different way (Returnibg all values of Tab1 and then matching values of both table)
Any suggestion plz how to resolve it?
Thanks in advance
Regards
Utsab Chattopadhyay
November 13, 2007 at 12:43 am
Sounds to me like Tab1 is nothing more than a subset of Tab2... that would cause such a return even though only an inner join were present.
--Jeff Moden
Change is inevitable... Change for the better is not.
November 13, 2007 at 10:15 am
Reckon Jeff's right.
Try this...
Select b.Col1, b.Col2, a.Col1, a.Col2
from Tab1 a join Tab2 b
on a.Col2 = b.Col2
... don't think you will find any rows where b.Col2 and a.Col2 don't match.
Then find rows in Tab1 which are not in Tab2:
SELECT b.Col1, b.Col2, a.Col1, a.Col2
FROM Tab1 a LEFT JOIN Tab2 b ON a.Col2 = b.Col2
WHERE b.Col2 IS NULL
And finally to confirm, find rows in Tab2 which are not in Tab1:
SELECT b.Col1, b.Col2, a.Col1, a.Col2
FROM Tab2 a LEFT JOIN Tab1 b ON a.Col2 = b.Col2
WHERE b.Col2 IS NULL
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply