Expected Output required

Posted by Self-Innovator under Sql Server on 4/9/2013 | Points: 10 | Views : 859 | Status : [Member] | Replies : 5
Hi,
I have two tables tb1 and tb2
when i ran the below statements i am getting an output but my expected out put is
select id from tb1
select id from tb2

out put
id
1
2
5
6

id
2
4
7
7


EXPECTED OUTPUT
id
2
5
4
7


i need an id values which is not common in both the tables

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Ankitsrist on: 4/9/2013 [Member] Starter | Points: 25

Up
0
Down
try this query
select distinct id from yourtablename


Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 4/9/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You mention id values which is not common in both the tables
but in your output how 2 is given.

If this post helps you mark it as answer
Thanks

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 4/9/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
SELECT DISTINCT id
FROM tb1
WHERE (id NOT IN
(SELECT DISTINCT id
FROM tb2))


Output
1
5
6


SELECT DISTINCT id
FROM tb2
WHERE (id NOT IN
(SELECT DISTINCT id
FROM tb1))




Output
4
7

If this post helps you mark it as answer
Thanks

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Murugavelmsc on: 4/10/2013 [Member] Starter | Points: 25

Up
0
Down
SELECT column1
FROM table1
INTERSECT
SELECT column1
FROM table2

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pandians on: 4/10/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Not Common from both table!
SELECT id FROM tb1

EXCEPT
SELECT id FROM tb2
Id

--
1
5
6
How it works!
1. Logically applies "DISTINCT SORT" on ID column in "Tb1" table
2. Reads data from "Tb1" row by row and applies with "Tb2" and Results the Non matched data from "Tb2"


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response