We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

vsuripeddi
vsuripeddi asked
on
Medium Priority
908 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
Comment
Watch Question

Commented:
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
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.

Or what would be the same thing:

Select * From Table_A;

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.

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?

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.

Commented:
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)
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
>>This is exactly what "INTERSECT" is. Why need more?
                                     ^
                                     ||
                       should be "MINUS"

sorry
vsuripeddi ,

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

:)
Acton

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.