SQL Query that can return intersecting data

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-06-12T18:19:51Z Indexed on 2010/06/12 18:22 UTC
Read the original article Hit count: 418

I have a hard time finding a good question title - let me just show you what I have and what the desired outcome is. I hope this can be done in SQL (I have SQL Server 2008).

1) I have a table called Contacts and in that table I have fields like these:

FirstName, LastName, CompanyName

2) Some demo data:

FirstName LastName CompanyName
John      Smith    Smith Corp
Paul      Wade  
Marc      Andrews  Microsoft
Bill      Gates    Microsoft
Steve     Gibbs    Smith Corp
Diane     Rowe     ABC Inc.

3) I want to get an intersecting list of people and companies, but companies only once. This would look like this:

Name
ABC Inc.
Bill Gates
Diane Rowe
John Smith   
Marc Andrews
Microsoft
Smith Corp
Steve Gibbs
Paul Wade

Can I do this with SQL? How?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server