vsuripeddi
asked on
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
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
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.
>>(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.
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.
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.
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)
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
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
^
||
should be "MINUS"
sorry
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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