67

Found the following in an Oracle-based application that we're migrating (generalized):

SELECT
    Table1.Category1,
    Table1.Category2,
    count(*) as Total,
    count(Tab2.Stat) AS Stat
FROM Table1, Table2
WHERE (Table1.PrimaryKey = Table2.ForeignKey(+))
GROUP BY Table1.Category1, Table1.Category2

What does (+) do in a WHERE clause? I've never seen it used like that before.

Jon Seigel
  • 12,035
  • 8
  • 56
  • 92
Jonathan Lonowski
  • 117,332
  • 31
  • 195
  • 197
  • 3
    There is also the legacy TSQL (SQL Server) `*=` and `=*` operators. Same boat. –  Jan 08 '11 at 08:05
  • 4
    I was looking for the meaning of these `(+)` in SQL queries for Oracle. And no way to search for `(+)` with Google. I headed to Stack Overflow, and the search engine of Stack Overflow gave me zero result for `(+)`. Which is wrong. There are at least two questions here on Stack Overflow about the meaning of `(+)`. It would be nice to correct the search engine. – Nicolas Barbulesco Jul 29 '13 at 14:55
  • 1
    Related: [Difference between Oracle's plus (+) notation and ansi JOIN notation?](http://stackoverflow.com/questions/1193654/difference-between-oracles-plus-notation-and-ansi-join-notation) – Jonathan Lonowski Apr 02 '14 at 03:25
  • 1
    Related: [Left Outer Join using + sign in Oracle 11g](http://stackoverflow.com/questions/6559261/left-outer-join-using-sign-in-oracle-11g) – Jonathan Lonowski Apr 02 '14 at 18:54

4 Answers4

100

Depending on which side of the "=" the "(+) is on, it denotes a LEFT OUTER or a RIGHT OUTER join (in this case, it's a left outer join). It's old Oracle syntax that is sometimes preferred by people who learned it first, since they like that it makes their code shorter.

Best not to use it though, for readability's sake.

SquareCog
  • 19,071
  • 8
  • 47
  • 63
  • 5
    Thanks for the extra tidbit about the orientation between `(+)` and `=`. – Jonathan Lonowski Jan 10 '09 at 01:06
  • 5
    - .5 for saying longer is more readable. is a++ harder to read than a = a + 1. I think it's much easier if you know what it means. If you don't know what it means, get out of my code. –  Feb 05 '09 at 21:28
  • 16
    @Mark -- I've been programming with Perl as my native language for the past 8 years. If you haven't learned the difference between short and obscure, please don't touch MY code! – SquareCog Feb 06 '09 at 00:01
  • 2
    Unfortunately the ANSI join syntax is not available in Oracle 8. So you need to stick with the (+) syntax in that case. – Cheekysoft Jul 20 '09 at 10:27
  • 2
    Unfortunately MATERIALIZED VIEW with FAST REFRESH ON COMMIT options does not support ANSI JOINS (Oracle 11). So you still need the (+) syntax in that case. – Sandre Dec 28 '16 at 20:39
26

As others have stated, the (+) syntax is obsolete, proprietary syntax that Oracle used for years to accomplish the same results as an OUTER JOIN. I assume they adopted their proprietary syntax before SQL-92 decided on the standard syntax.

The equivalent query to the one you showed, using standard SQL OUTER JOIN syntax (which is now supported by all major RDBMS implementations) would be the following:

SELECT
    Table1.Category1,
    Table1.Category2,
    COUNT(*) AS Total,
    COUNT(Table2.Stat) AS Stat
FROM Table1
  LEFT OUTER JOIN Table2 ON (Table1.PrimaryKey = Table2.ForeignKey)
GROUP BY Table1.Category1, Table1.Category2;

Which means:

  • All rows from Table1 are included in the query result.
  • Where there are matching rows in Table2, include those rows (repeating content from Table1 if there are multiple matching rows in Table2).
  • Where there are no matching rows in Table2, use NULL for all of Table2's columns in the query result.
Bill Karwin
  • 499,602
  • 82
  • 638
  • 795
5

It's a non ANSI left outer join notation. Starting with Oracle9i, the confusing outer join syntax using the ‘(+)’ notation has been superseded by ISO 99 outer join syntax.

Otávio Décio
  • 72,200
  • 17
  • 159
  • 225
3

A noteworthy consideration is that the classic Oracle notation is not intuitive and is best avoided from a code clarity and maintainability perspective.

To illustrate this point, I have included this example.

To achieve a LEFT outer join between tables A and B one would expect the table on the left which is A should have the (+) operator next to it. This would make sense as we want to denote we would include all rows of A regardless of the success in join criteria with B. However this is not the case and the join is achieved as follows

select b.age, a.name
from Employees a, EmployeeUNI b
where a.id = b.id(+)

I prefer the ANSI SQL version which is explicit:

select b.age, a.name
From Employees a 
    LEFT outer join EmployeeUNI b
on a.id = b.id

Both methods result in the same output however the ANSI approach does not come with the risk of the novice programmer mistakenly putting the (+) in the wrong place.

Noah Broyles
  • 864
  • 1
  • 9
  • 24