Need a SQL Query with out using "not in"
-
Sunday, March 17, 2013 5:44 AM
Hi,
I have two tables called TableA & TableB
TableA TableB
---------- ----------
ID ID
---- ----------
10 11
11 12
12 13Need help to write a SQL query without using "not in" to get the below Out Put
------------------
10
11
12
13
Adv Thanks,
Bijay
All Replies
-
Sunday, March 17, 2013 6:04 AM
select ID from TableA
UNION
Select ID from TableB
declare @a table (sno int) insert into @a values (1),(2),(3) declare @b table(sno int) insert into @b values (2),(3),(4) select sno from @a union select sno from @b
Hope it Helps!!
- Edited by Stan210 Sunday, March 17, 2013 6:10 AM
- Proposed As Answer by Kalman TothMicrosoft Community Contributor, Moderator Sunday, March 17, 2013 7:37 AM
- Marked As Answer by Allen Li - MSFTModerator Monday, March 25, 2013 5:29 AM
-
Sunday, March 17, 2013 7:41 AMAnswerer
create table #t1 (id int)
create table #t2 (id int)
insert into #t1 values (10),(11),(12)
insert into #t1 values (11),(12),(13)
select distinct #t1.id from #t1 full outer join #t2
on #t1.id=#t2.idBest Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Blog: Large scale of database and data cleansing
Remote DBA Services: Improves MS SQL Database Performance
- Marked As Answer by Allen Li - MSFTModerator Monday, March 25, 2013 5:29 AM
-
Sunday, March 17, 2013 4:44 PMModeratorI do not see why would you need NOT IN here at all. It is a simple UNION query to get distinct rows from both tables.
For every expert, there is an equal and opposite expert. - Becker's Law
My blog

