Friday, April 17, 2009

UNION & UNION ALL

CONTACT DATA:
ACCOUNT_ID | CONTACT_NAME| JOB_TITLE
---------------------------------------------------
10000000001 | ABC | CEO
10000000001 | DEF | CIO
10000000002 | OPQ | CEO
10000000002 | XYZ | HR

SELECT JOB_TITLE
FROM CONTACT WHERE ACCOUNT_ID = 10000000001
UNION
SELECT JOB_TITLE
FROM CONTACT WHERE ACCOUNT_ID = 10000000002

Result:
JOB_TITLE
------------
CEO
CIO
HR

SELECT JOB_TITLE
FROM CONTACT WHERE ACCOUNT_ID = 10000000001
UNION ALL
SELECT JOB_TITLE
FROM CONTACT WHERE ACCOUNT_ID = 10000000002

Result:
JOB_TITLE
-------------
CEO
CIO
CEO
HR

No comments:

Post a Comment