Is there a syntax like "UNION INTERSECT" in SQL (Oracle specific)

Experts,

I am looking to select all rows in table A but not in Table B.

so currently i can get to select this result set by doing the following query

(SELECT * from table A
MINUS
SELECT * from table B)
UNION ALL
(SELECT * from table A
INTERSECT
SELECT * from table B)



I am wondering if there is a better way/ syntax to do this in Oracle ,
say using something like UNION and INTERSECT together
to achieve the above result set

(Basically I want to get all the rows in table A
but do not want to lose any rows in table A  
--- when we do the MINUS we will lose the common rows)


Would like to know some tips on set operations to do nifty things

Thanks a bunch
xyz
vsuripeddiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MetanilCommented:
let us assume that u have a distict field called 'distinct_field'

Then your query should look like this:
SELECT a.*
FROM   TABLEA a
WHERE   NOT  EXISTS (SELECT 1
                     FROM   TABLEB b
                     WHERE  a.DISTINCT_FIELD = b.DISTINCT_FIELD);

Metanil
0
jazzylineCommented:
vsuripeddi,

>>(Basically I want to get all the rows in table A but do not want to lose any rows in table A --- when we do the MINUS we will lose the common rows)

If that's the case then you don't really need to complicate the query with the set operators. Assuming that there is  a common ralational key between the two tables, you can simply do this:

SELECT * FROM Table_1 a, Table_2 b
WHERE a.key_id(+) = b.key_id

This query will select all rows from table_1 and matching rows from table_2. I think that's what your actual requirement is. The (+) operators selects all rows from table_1.
0
MikeOM_DBACommented:

Or what would be the same thing:

Select * From Table_A;

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

jazzylineCommented:
sorry the query should have been :

SELECT * FROM Table_1 a, Table_2 b
WHERE a.key_id = b.key_id (+)

MikeOM_DBA- That was my first impression too. But his/her requirement seems that all rows rows from Table_A should be returned and also those rows returned which are present in Table_B as well as Table_A, which would suggest that Table_B is supposed to be the deficient table.
0
MikeOM_DBACommented:

Jazzyline - Except for the number of columns that would be returned by your query (a.*, b.*) the result will be all rows from Table_1 regardless if they exist (or not) in Table_2 (outer join).

The only advantage is that you can select columns from both tables when row (key) exists in both.

Or, maybe the requirement needs more clarity?

0
jazzylineCommented:
MikeOM_DBA - I agree
Per Author >> Basically I want to get all the rows in table A but do not want to lose any rows in table A --- when we do the MINUS we will lose the common rows.
I'm sure he/she is aware of select * from A as it is already in the query. The lead me to beleive that he/she wants to retrieve all rows from table_A with fields from table_B. Otherwise it would be a simple  select * from A where interaction with table B is not required at all.

When I re-read the question, it says in the begining>>I am looking to select all rows in table A but not in Table B.
which would be totally different result set.

SELECT * FROM table_A a
WHERE  a.key_id IN ( SELECT m.key_id FROM table_A m
                                MINUS
                                SELECT n.key_id FROM table_B n)

Perhaps the requirement needs more than clarification.
0
KJltCommented:
I don't know will it be better performance but you can try:

SELECT * from table A
MINUS
(SELECT * from table B
MINUS
SELECT * from table A)
0
actonwangCommented:
Everybody,

          Let me get it straight:

         >>I am looking to select all rows in table A but not in Table B.
          This is exactly what "INTERSECT" is. Why need more?

>> SELECT * from table A
MINUS
SELECT * from table B)
UNION ALL
(SELECT * from table A
INTERSECT
SELECT * from table B)
           The result is table A itself, isn't it?

Acton
0
actonwangCommented:
>>This is exactly what "INTERSECT" is. Why need more?
                                     ^
                                     ||
                       should be "MINUS"

sorry
0
actonwangCommented:
vsuripeddi ,

           What you need is just a simple "MINUS", isn't it ?

:)
Acton
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.