Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving records from table A that are not in Table B

Status
Not open for further replies.

johncnoland

Programmer
Jun 6, 2007
6
US
Table A is a master set of keys values (Date, ID, City).

The records in Table B represent a sub-set of the key values in Table A.

How can I retrieve the records in Table A that are not in Table B?

Thought of using a sub-query but don't those only return a single column?

Using SQL 2000.
 
Code:
select a.* from tablea a
left join tableb b on a.Date = b.Date
and a.id = b.id
and a.city =b.city
where a.id is null



example
Code:
CREATE TABLE TestTable1 (id1 int) 
CREATE TABLE TestTable2 (id2 int) 

INSERT TestTable1 VALUES(1) 
INSERT TestTable1 VALUES(2) 
INSERT TestTable1 VALUES(3) 


INSERT TestTable2 VALUES(1) 
INSERT TestTable2 VALUES(2) 

SELECT t1.* 
FROM TestTable1 t1 
LEFT JOIN TestTable2 t2 ON t2.id2 = t1.id1 
WHERE t2.id2 IS NULL

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
SQLBlog.com, Google Interview Questions
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top