Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Oracle's plus (+) notation and ansi JOIN notation?

What's the difference between using oracle's plus notation (+) over the ansi standard join notation?

Is there a difference in performance?

Is the plus notation deprecated?

like image 830
Franz See Avatar asked Jul 28 '09 12:07

Franz See


People also ask

What is the difference between ANSI SQL and Oracle SQL?

ANSI is an industry standard. Some Oracle features are not ANSI compliant, either because they were added before the standard was defined (like DECODE) or because a commercial product like Oracle needs to be able to add new features (like MODEL). Basically there is syntax difference between both of them.

What is ANSI joins in Oracle?

The ANSI join syntax was introduced in Oracle 9i. It has a number of advantages over the original syntax. It reads more like English, so it is much clearer. The tables and join conditions are all kept together in the FROM clause, so the WHERE clause only contains filters, not join conditions.

What is (+) in Oracle join?

The (+) operator indicates an outer join. This means that Oracle will still return records from the other side of the join even when there is no match.

What is ANSI style join?

If the FROM clause specifies more than one table reference, the query can join rows from several tables or views. A join condition specifies a relationship between at least one column from each table to be joined.


2 Answers

AFAIK, the (+) notation is only present for backwards compatibility because Oracle debuted it before the ANSI standard for joins was put in place. It's specific to Oracle and you should avoid using it in new code when there's an equivalent standards-compliant version available.

It seems there are differences between the two, and the (+) notation has restrictions that the ANSI join syntax does not have. Oracle themselves recommend that you not use the (+) notation. Full description here in the Oracle® Database SQL Language Reference 11g Release 1 (11.1):

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:

  • You cannot specify the (+) operator in a query block that also contains FROM clause join syntax.
  • The (+) operator can appear only in the WHERE clause or, in the context of left-correlation (when specifying the TABLE clause) in the FROM clause, and can be applied only to a column of a table or view.
  • If A and B are joined by multiple join conditions, then you must use the (+) operator in all of these conditions. If you do not, then Oracle Database will return only the rows resulting from a simple join, but without a warning or error to advise you that you do not have the results of an outer join.
  • The (+) operator does not produce an outer join if you specify one table in the outer query and the other table in an inner query.
  • You cannot use the (+) operator to outer-join a table to itself, although self joins are valid.

For example, the following statement is not valid:

SELECT employee_id, manager_id FROM employees WHERE employees.manager_id(+) = employees.employee_id; 

However, the following self join is valid:

SELECT e1.employee_id, e1.manager_id, e2.employee_id FROM employees e1, employees e2 WHERE e1.manager_id(+) = e2.employee_id; 
  • The (+) operator can be applied only to a column, not to an arbitrary expression. However, an arbitrary expression can contain one or more columns marked with the (+) operator.
  • A WHERE condition containing the (+) operator cannot be combined with another condition using the OR logical operator.
  • A WHERE condition cannot use the IN comparison condition to compare a column marked with the (+) operator with an expression.

If the WHERE clause contains a condition that compares a column from table B with a constant, then the (+) operator must be applied to the column so that Oracle returns the rows from table A for which it has generated nulls for this column. Otherwise Oracle returns only the results of a simple join.

In a query that performs outer joins of more than two pairs of tables, a single table can be the null-generated table for only one other table. For this reason, you cannot apply the (+) operator to columns of B in the join condition for A and B and the join condition for B and C. Refer to SELECT for the syntax for an outer join.

like image 183
nagul Avatar answered Sep 22 '22 21:09

nagul


The most comprehensive answer obviously is the one by nagul.

An addition for those who are looking for quick translation/mapping to the ANSI syntax:

-- -- INNER JOIN -- SELECT * FROM EMP e INNER JOIN DEPT d ON d.DEPTNO = e.DEPTNO;   -- Synonym in deprecated oracle (+) syntax SELECT * FROM EMP e,      DEPT d WHERE d.DEPTNO = e.DEPTNO;  -- -- LEFT OUTER JOIN -- SELECT * FROM EMP e LEFT JOIN DEPT d ON d.DEPTNO = e.DEPTNO;   -- Synonym in deprecated oracle (+) syntax SELECT * FROM EMP e,      DEPT d WHERE d.DEPTNO (+) = e.DEPTNO;  -- -- RIGHT OUTER JOIN -- SELECT * FROM EMP e RIGHT JOIN DEPT d ON d.DEPTNO = e.DEPTNO;  -- Synonym in deprecated oracle (+) syntax SELECT * FROM EMP e,      DEPT d WHERE d.DEPTNO = e.DEPTNO(+);  -- -- CROSS JOIN -- SELECT * FROM EMP e CROSS JOIN DEPT d;   -- Synonym in deprecated oracle (+) syntax SELECT * FROM EMP e,      DEPT d;  -- -- FULL JOIN -- SELECT * FROM EMP e FULL JOIN DEPT d ON d.DEPTNO = e.DEPTNO;  -- Synonym in deprecated oracle (+) syntax !NOT WORKING! SELECT * FROM EMP e,      DEPT d WHERE d.DEPTNO (+) = e.DEPTNO(+); 
like image 33
schnatterer Avatar answered Sep 22 '22 21:09

schnatterer