Multiple column Union Query without duplicates

Posted by Adam Halegua on Stack Overflow See other posts from Stack Overflow or by Adam Halegua
Published on 2012-12-18T16:21:46Z Indexed on 2012/12/18 17:03 UTC
Read the original article Hit count: 152

Filed under:
|
|

I'm trying to write a Union Query with multiple columns from two different talbes (duh), but for some reason the second column of the second Select statement isn't showing up in the output. I don't know if that painted the picture properly but here is my code:

Select empno, job
From EMP
Where job = 'MANAGER'
Union
  Select empno, empstate
  From EMPADDRESS
  Where empstate = 'NY'
  Order By empno

The output looks like:

EMPNO   JOB
4600    NY
5300    MANAGER
5300    NY
7566    MANAGER
7698    MANAGER
7782    MANAGER
7782    NY
7934    NY
9873    NY

Instead of 5300 and 7782 appearing twice, I thought empstate would appear next to job in the output. For all other empno's I thought the values in the fields would be (null). Am I not understanding Unions correctly, or is this how they are supposed to work?

Thanks for any help in advance.

© Stack Overflow or respective owner

Related posts about sql

Related posts about Oracle