Tuesday, May 5, 2009

Oracle SQL: Traditional Outer Join Syntax

I was teaching Oracle 11g SQL Fundamentals and PL/SQL Fundamentals course when one of my students asked me a startling question right after I've discussed JOIN topic.

"What's the difference of using the JOIN keyword and using the (+) for joining of tables?"

Then, he showed me a SQL script using the (+) syntax and explained to me that his mentor taught him to use this syntax and that they are also using this SQL syntax in all their systems and applications.

For a while, I wondered... because I've never encountered this (+) syntax with Oracle 9i, 10g and 11g books and course materials. I told him, maybe it was the old JOIN syntax being used by earlier versions of Oracle.

To support my wild guess... that it is really an old JOIN syntax still supported by Oracle, I started to google about it and I landed on Oreilly's
Oracle SQL*Plus Pocket Reference, 2nd Edition, by Jonathan Gennick, sample chapter excerpt: http://oreilly.com/catalog/orsqlpluspr2/chapter/ch01.html


According to the book, the (+) syntax for joining tables is an old syntax for outer joins until Oracle 8i. An outer join returns rows for one table, even when there are no matching rows in the other. You specify an outer join in Oracle by placing a plus sign (+) in parentheses following the column names from the optional table in your WHERE clause. For example:
SELECT ut.table_name, uc.constraint_name
FROM user_tables ut, user_constraints uc
WHERE ut.table_name = uc.table_name(+)

The (+) after uc.table_name makes the user_constraint table optional. The query returns all tables, and where there are no corresponding constraint records, Oracle supplies a null in the constraint name column. Full outer join is not supported using the (+) syntax.

At the release of Oracle 9i and later versions, full outer join is already supported using the JOIN syntax. 

SELECT ut.table_name, uc.constraint_name
FROM user_tables ut FULL OUTER JOIN user_constraints uc
ON ut.table_name = uc.table_name

Although you could still use the old (+) syntax for outer joins for backward compatibility, Oracle strongly encourage to use the new JOIN syntax for full outer join support. 

No comments: