Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle "(+)" Operator

I am checking some old SQL Statements for the purpose of documenting them and probably enhancing them.

The DBMS is Oracle

I did not understand a statement which read like this:

select ... from a,b where a.id=b.id(+) 

I am confused about the (+) operator, and could not get it at any forums... (searching for + within quotes didn't work either).

Anyway, I used 'Explain Plan' of SQLDeveloper and I got an output saying that HASH JOIN, RIGHT OUTER, etc.

Would there be any difference if I remove the (+) operator at the end of the query? Does the database have to satisfy some condition (like having some indexes, etc.) before (+) can be used?? It would be greatly helpful if you can provide me a simple understanding, or some good links where I can read about this.

Thanks!

like image 340
Sekhar Avatar asked Oct 26 '10 04:10

Sekhar


2 Answers

That's Oracle specific notation for an OUTER JOIN, because the ANSI-89 format (using a comma in the FROM clause to separate table references) didn't standardize OUTER joins.

The query would be re-written in ANSI-92 syntax as:

   SELECT ...      FROM a LEFT JOIN b ON b.id = a.id 

This link is pretty good at explaining the difference between JOINs.


It should also be noted that even though the (+) works, Oracle recommends not using it:

Oracle recommends that you use the FROM clause OUTER JOIN syntax rather than the Oracle join operator. Outer join queries that use the Oracle join operator (+) are subject to the following rules and restrictions, which do not apply to the FROM clause OUTER JOIN syntax:

like image 172
OMG Ponies Avatar answered Sep 19 '22 22:09

OMG Ponies


In Oracle, (+) denotes the "optional" table in the JOIN. So in your query,

SELECT a.id, b.id, a.col_2, b.col_2, ... FROM a,b WHERE a.id=b.id(+) 

it's a LEFT OUTER JOIN of table 'b' to table 'a'. It will return all data of table 'a' without losing its data when the other side (optional table 'b') has no data.

Diagram of Left Outer Join

The modern standard syntax for the same query would be

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a LEFT JOIN b ON a.id=b.id 

or with a shorthand for a.id=b.id (not supported by all databases):

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a LEFT JOIN b USING(id) 

If you remove (+) then it will be normal inner join query

Older syntax, in both Oracle and other databases:

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a,b WHERE a.id=b.id 

More modern syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a INNER JOIN b ON a.id=b.id 

Or simply:

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a JOIN b ON a.id=b.id 

Diagram of Inner Join

It will only return all data where both 'a' & 'b' tables 'id' value is same, means common part.

If you want to make your query a Right Join

This is just the same as a LEFT JOIN, but switches which table is optional.

Diagram of Right Outer Join

Old Oracle syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a,b WHERE a.id(+)=b.id 

Modern standard syntax:

SELECT  a.id, b.id, a.col_2, b.col_2, ... FROM a RIGHT JOIN b ON a.id=b.id 

Ref & help:

https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:6585774577187

Left Outer Join using + sign in Oracle 11g

https://www.w3schools.com/sql/sql_join_left.asp

like image 41
Md. Salman Fahad Famous Avatar answered Sep 21 '22 22:09

Md. Salman Fahad Famous