Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2006-03-20
12
Medium Priority
?
895 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:vsuripeddi
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 9

Expert Comment

by:Metanil
ID: 16244326
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
 
LVL 3

Expert Comment

by:jazzyline
ID: 16248973
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16251900

Or what would be the same thing:

Select * From Table_A;

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:jazzyline
ID: 16252552
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 16262114

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
 
LVL 3

Expert Comment

by:jazzyline
ID: 16286080
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
 
LVL 2

Expert Comment

by:KJlt
ID: 16298407
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
 
LVL 19

Expert Comment

by:actonwang
ID: 16350956
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
 
LVL 19

Expert Comment

by:actonwang
ID: 16350963
>>This is exactly what "INTERSECT" is. Why need more?
                                     ^
                                     ||
                       should be "MINUS"

sorry
0
 
LVL 19

Accepted Solution

by:
actonwang earned 1000 total points
ID: 16350972
vsuripeddi ,

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

:)
Acton
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question