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
 SQL Query

Author  Topic 

dim
Yak Posting Veteran

57 Posts

Posted - 2011-05-20 : 16:29:47

Hi,

I have two tables A AND B. Both the tables have first_name and last_name fields. I want to find out that which combination of first_name and last_name are same in both the tables.

For exampple : John Smith in table A should match to John Smith in table B

The query that I use gives me cross join with different combination from other table B

My query:

select a.first_name,
a.last_name,
b.first_name,
b.last_name
from a, b
where a.first_name= b.first_name
and a.last_name =b.last_name


The resulting query gives me results but also cross join results...
like John Smith John Smith
John Smith John Travolta


Please advice..

Thanks,



Dp

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-05-20 : 16:42:54
Another method:

SELECT first_name, last_name FROM a
INTERSECT
SELECT first_name, last_name FROM b
Go to Top of Page

kazi
Starting Member

8 Posts

Posted - 2011-05-21 : 02:07:46
@dim I am not finding any mistake in your query.
The query can not generate the result like 'John Smith John Travolta'

Kazi
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-05-21 : 05:55:52
That is what I thought too.



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jfarrugia
Yak Posting Veteran

55 Posts

Posted - 2011-05-21 : 07:40:48
Try

select a.first_name,
a.last_name,
b.first_name,
b.last_name
from a join b on a.first_name= b.first_name and a.last_name =b.last_name


Your original query seems correct though!

Your key to software development =>http://www.itexposed.com
Go to Top of Page
   

- Advertisement -