Solved

t-sql complement

Posted on 2016-11-08
8
36 Views
Last Modified: 2016-11-08
I saw this example.

http://stackoverflow.com/questions/2686254/how-to-select-all-records-from-one-table-that-do-not-exist-in-another-table

--select all records from one table that do not exist in another table?
SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL


how would I get the complement of the above query?
What I mean is select all records from table t1 that do exist in table2?
0
Comment
Question by:maqskywalker
8 Comments
 
LVL 21

Assisted Solution

by:Randy Poole
Randy Poole earned 83 total points
ID: 41879037
SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE NOT t2.name IS NULL
0
 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41879043
You can use something more near to the English language:
SELECT t1.name
 FROM table1 t1
WHERE NOT EXISTS (SELECT 1
        FROM table2 t2
        WHERE t2.name t2.name = t1.name)

Open in new window

0
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 83 total points
ID: 41879045
Another way:

SELECT t1.name
FROM table1 t1
JOIN table2 t2 ON t2.name = t1.name

A join inherently returns only those rows that exist in both tables.
0
 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 251 total points
ID: 41879047
Correction:
SELECT t1.name
FROM table1 t1
WHERE NOT EXISTS (SELECT 1
        FROM table2 t2
        WHERE t2.name = t1.name)

Open in new window

0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 46

Expert Comment

by:Vitor Montalvão
ID: 41879049
Oh, just realized now is for EXIST in table2. Just read it in a negative away :(
So better solution is provided by Steve Wales.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41879050
In addition to the above correct answers, go to images.google.com and do a search for 'SQL JOIN', and you'll see all sorts of handy images that are Venn diagrams of two or more tables, and the T-SQL needed to pull them off.
1
 
LVL 24

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 83 total points
ID: 41879067
Try..You can also use Except..

SELECT t1.name
FROM table1 t1

EXCEPT 

SELECT t2.name 
FROM table2 t2

Open in new window

0
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41879079
thanks.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now