Ajay Arora
Ajay Arora

Reputation: 1

finding unmatched data from two tables

I'm trying write a query to find records which don't have a matching record in another table.

For example, I have a two tables whose structures looks something like this:

Table1

Address ID | Address Type
AD7233654242 | Condo
AD7233654242 | Condo
AD7233654243 | Apartment
AD7233654244 | Condo

Table2

Address ID | Address Type
AD7233654242 | Condo
AD7233654242 | Apartment
AD7233654243 | Apartment
AD7233654244 | Condo

Based on the data from above you'll notice address ID AD7233654242 has a mismatch Address Type. Table A shows Condo and Table B shows Apartment. So in the query result I want to dislay the Address ID and Address Type from both of the tables.

Any suggestions on a query to do this?

Upvotes: 0

Views: 64

Answers (4)

Alex Kudryashev
Alex Kudryashev

Reputation: 9460

You can query matching and not matching records using join type. Below are examples of different joins.

declare @tbl1 table(AddressID varchar(20), AddressType varchar(50))
declare @tbl2 table(AddressID varchar(20), AddressType varchar(50))

insert @tbl1(AddressID,AddressType) values
('AD7233654242', 'Condo'),
('AD7233654242', 'Condo'),
('AD7233654243', 'Apartment'),
('AD7233654244', 'Condo'),
('AD7233654245', 'Condo')--my sample

insert @tbl2(AddressID,AddressType) values
('AD7233654242', 'Condo'),
('AD7233654242', 'Apartment'),
('AD7233654243', 'Apartment'),
('AD7233654244', 'Condo')

--records in @tbl1 matching @tbl2
select t1.AddressID addrId_1, t1.AddressType addrType_1, t2.AddressID addrId_2, t2.AddressType addrType_2
from @tbl1 t1 -- 1st or left table
inner join @tbl2 t2 --2nd or right table
on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType

--records in @tbl1 not matching @tbl2
select t1.AddressID, t1.AddressType
from @tbl1 t1
left join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t2.AddressID is null

--records in @tbl2 not matching @tbl1
select t2.AddressID, t2.AddressType
from @tbl1 t1
right join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t1.AddressID is null

--all mismatches
select t1.AddressID addrId_1, t1.AddressType addrType_1, t2.AddressID addrId_2, t2.AddressType addrType_2
from @tbl1 t1
full join @tbl2 t2 on t1.AddressID=t2.AddressID and t1.AddressType=t2.AddressType
where t1.AddressID is null or t2.AddressID is null

Upvotes: 1

kiran gadhe
kiran gadhe

Reputation: 743

I guess this is what you are asking..

SELECT * from table1 T1
where exists (
SELECT * from table2 T2
where T1.AddressID = T2.AddressID
and T1.AddressType<>T1.AddressType)

UNION 

SELECT * from table1 T2
where exists (
SELECT * from table2 T1
where T1.AddressID = T2.AddressID
and T1.AddressType<>T1.AddressType)

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

You can try using left join and null in where condition

SELECT t1.AddressID, t1.AddressType,t2.AddressType
FROM Table1 t1
left JOIN Table2 t2
    ON t1.AddressID = t2.AddressID AND t1.AddressType=t2.AddressType
where t2.AddressID is null

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

A simple join should work here:

SELECT
    t1.AddressID,
    t1.AddressType,
    t2.AddressType
FROM Table1 t1
INNER JOIN Table2 t2
    ON t1.AddressID = t2.AddressID AND t1.AddressType <> t2.AddressType;

This would actually return two records for AD7233654242. If you have a certain expected output, we can modify the above query using that logic.

Upvotes: 0

Related Questions