By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,159 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,159 IT Pros & Developers. It's quick & easy.

how to bring back the distinct values in single column from two tables

P: n/a
12.) Now you have two different tables - each with two columns.

Table #1

Single Column

2 rows with a value equal to 1 and 2

Table #2

Single column

2 rows with a value equal to 2 and 4

Construct a statement returning in a single column all the values
contained, but not the common values.

This is another question that I got in an interview, I missed
it.......

Thanks,

Tim
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
CREATE TABLE T1 (x INTEGER PRIMARY KEY)
INSERT INTO T1 VALUES (1)
INSERT INTO T1 VALUES (2)

CREATE TABLE T2 (x INTEGER PRIMARY KEY)
INSERT INTO T2 VALUES (2)
INSERT INTO T2 VALUES (4)
Method 1:

SELECT COALESCE(T1.x, T2.x)
FROM T1
FULL JOIN T2
ON T1.x = T2.x
WHERE T1.x IS NULL OR T2.x IS NULL
Method 2:

SELECT x
FROM
(SELECT x
FROM T1
UNION ALL
SELECT x
FROM T2) AS T
GROUP BY x
HAVING COUNT(*)=1

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.