Home » SQL & PL/SQL » SQL & PL/SQL » What is the difference between MINUS and LEFT outer join (Oracle 10g, Windows)
What is the difference between MINUS and LEFT outer join [message #562074] Fri, 27 July 2012 15:31 Go to next message
nathb
Messages: 77
Registered: March 2011
Member
Howdy my good folks,

What is the fundamental difference between MINUS keyword and LEFT outer join in Oracle.

I am confused because, I can achive same results using either one of them.

Please help.
thanks
nath
Re: What is the difference between MINUS and LEFT outer join [message #562075 is a reply to message #562074] Fri, 27 July 2012 15:37 Go to previous messageGo to next message
BlackSwan
Messages: 20086
Registered: January 2009
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/

>I can achive same results using either one of them.
please post reproducible test case that shows above is true.
Re: What is the difference between MINUS and LEFT outer join [message #562079 is a reply to message #562075] Fri, 27 July 2012 15:51 Go to previous messageGo to next message
nathb
Messages: 77
Registered: March 2011
Member
I tried googling..but no good explanation. So I thought I will ask here. But if you think this is not a valid question, please delete it.
Re: What is the difference between MINUS and LEFT outer join [message #562081 is a reply to message #562079] Fri, 27 July 2012 16:09 Go to previous messageGo to next message
John Watson
Messages: 3102
Registered: January 2010
Location: Global Village
Senior Member
If you give examples of your code, perhaps it can be explained. There are often several equivalent SQL statements.
Re: What is the difference between MINUS and LEFT outer join [message #562082 is a reply to message #562081] Fri, 27 July 2012 16:17 Go to previous messageGo to next message
nathb
Messages: 77
Registered: March 2011
Member
Using MINUS:

select col1 from Table1 where as_of_Date = '31-dec-2010'
minus
select col1 Table2

Using outer join:

select table1.col1, table2,col2
from qtable1, table2
where table1.col1 = table2.col1(+)
and table1.as_of_date = '31-DEC-2010'
and table2.deal_number is null
Re: What is the difference between MINUS and LEFT outer join [message #562084 is a reply to message #562082] Fri, 27 July 2012 16:37 Go to previous messageGo to next message
cookiemonster
Messages: 9135
Registered: September 2008
Location: Rainy Manchester
Senior Member
Those two selects aren't at all equivalent.
Not even a little bit.
Is table2 empty by any chance?
Re: What is the difference between MINUS and LEFT outer join [message #562085 is a reply to message #562084] Fri, 27 July 2012 16:40 Go to previous messageGo to next message
nathb
Messages: 77
Registered: March 2011
Member
so the minus in the first query will check only on column 'Col1' in the table1 and table2?
Re: What is the difference between MINUS and LEFT outer join [message #562087 is a reply to message #562085] Fri, 27 July 2012 17:05 Go to previous messageGo to next message
matthewmorris68
Messages: 129
Registered: May 2012
Location: Orlando, FL
Senior Member
As John indicated, it is possible to have two SQL statements that will return the same data. That doesn't mean they are doing the same thing or that they will return the same data given slightly different conditions. I set up an example case:

CREATE TABLE table_A (
  col1     NUMBER,
  col2     VARCHAR2(1)
);
 
CREATE TABLE table_B (
  col1     NUMBER,
  col2     VARCHAR2(1)
);

INSERT INTO table_A VALUES (1, 'a');
INSERT INTO table_A VALUES (2, 'b');
INSERT INTO table_A VALUES (3, 'c');
 
INSERT INTO table_B VALUES (2, 'B');
INSERT INTO table_B VALUES (3, 'C');
INSERT INTO table_B VALUES (4, 'D');



If you perform a LEFT JOIN on table A and B by column 1, you'll get the following results:

SELECT a.col1, a.col2
FROM  table_A a
      LEFT JOIN table_B b
      ON a.col1 = b.col1;
      
COL1 COL2
---- ----
   2 b    
   3 c    
   1 a    


If you perform a MINUS operation of col1 and col2 (A minus B), you'll get the same results (albeit a different order since I didn't use ORDER BY in both queries):

SELECT a.col1, a.col2
FROM  table_A a
MINUS
SELECT b.col1, b.col2
FROM  table_B b

COL1 COL2
---- ----
   1 a    
   2 b    
   3 c   



The reason the same results are being returned are for entirely different reasons. Basically all three rows from table_A are being returned in both cases. In the first, the LEFT join specifically indicates return all rows that exist in A, regardless of whether they exist in B. In the second query, the MINUS operation only removes rows where the data in B exactly matches the data in A. Since that never happens, all rows from A get returned.

If we swap the query to be Table_B minus Table_A, then the results are completely different:

SELECT b.col1, b.col2
FROM  table_B b
MINUS
SELECT a.col1, a.col2
FROM  table_A a;

COL1 COL2
---- ----
   2 B    
   3 C    
   4 D    


Alternately, if I were to insert a new row in B that matched a row in A and then re-ran the first two queries, they would produce different results:

INSERT INTO table_B VALUES (1, 'a');

SELECT a.col1, a.col2
FROM  table_A a
      LEFT JOIN table_B b
      ON a.col1 = b.col1;
      
COL1 COL2
---- ----
   2 b    
   3 c    
   1 a    
   
SELECT a.col1, a.col2
FROM  table_A a
MINUS
SELECT b.col1, b.col2
FROM  table_B b

COL1 COL2
---- ----
   2 b    
   3 c    

Re: What is the difference between MINUS and LEFT outer join [message #562089 is a reply to message #562087] Fri, 27 July 2012 17:39 Go to previous message
nathb
Messages: 77
Registered: March 2011
Member
Thank you so much for your explanation.

Appreciate greatly.
Previous Topic: Date calculation
Next Topic: Project
Goto Forum:
  


Current Time: Mon May 20 04:13:54 CDT 2013

Total time taken to generate the page: 0.21252 seconds