question

innovator avatar image
innovator asked

Query to find unmatched records

Hi All, Please help me with the below mentioned scenario, Data is-> Table A(int id,name varchar(20) Table B(int id,name varchar(20) A 1,'john' 2,'paul; 3,'Sid' 8,'pol' B 1,'john' 2,'paul; 4,'Max' 6,'Pat' 3,'Sid' I wanna retrieve o/p as below 4,'Max' 6,'Pat', 8,'pol' That is I want unmatched records of both table A and B.
sqljoin
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

innovator avatar image innovator commented ·
Thnx usman for ur comment!
0 Likes 0 ·
Usman Butt avatar image
Usman Butt answered
The combination of FULL OUTER JOIN and IS NULL would do the job for you CREATE Table #A(id INT ,name varchar(20) ) Create Table #B( id int ,name varchar(20)) INSERT [#A] ( id ,[name] ) SELECT 1,'john' UNION ALL SELECT 2,'paul' UNION ALL SELECT 3,'Sid' UNION ALL SELECT 8,'pol' INSERT [#B] ( id ,[name] ) SELECT 1,'john' UNION ALL SELECT 2,'paul' UNION ALL SELECT 4,'Max' UNION ALL SELECT 6,'Pat' UNION ALL SELECT 3,'Sid' SELECT ISNULL([A].[id], [B].[id]), ISNULL([A].[name], [B].[name]) FROM [#A] AS A FULL OUTER JOIN [#B] AS B ON [A].[name] = [B].[name] WHERE [A].[name] IS NULL OR [B].[name] IS NULL DROP TABLE [#A],[#B]
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image
ThomasRushton answered
There's a handy reference image going round that may help with these problems... ![Handy reference image][1] [1]: http://media-cdn.pinterest.com/upload/48695239690455741_FTyCDEus_f.jpg
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
+1 nice, even if somewhat garish :)
0 Likes 0 ·
ThomasRushton avatar image ThomasRushton ♦♦ commented ·
Don't blame me - I didn't create it!
0 Likes 0 ·
innovator avatar image innovator commented ·
Thanx thomas!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.