Sub topic 6
Converting (+) Queries to ANSI Joins
Assume you know basic SELECT, WHERE, and ORDER BY clauses, table aliases, primary and foreign keys, and the meaning of NULL. This chapter focuses on reading Oracle's older outer join operator, (+), and rewriting it with ANSI join syntax.
Identify the optional table
In legacy syntax, the (+) marker goes on the table column that may not have a matching row. Consider this query:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);The marker is attached to the employees column. Therefore, departments is the preserved table, and every department appears in the result. If a department has no employees, e.last_name is NULL.
The equivalent ANSI query is:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id;The table before LEFT OUTER JOIN is preserved. The table after it is optional.
| Legacy form | ANSI form | Preserved table |
|---|---|---|
| `d.id = e.id(+)` | `d LEFT JOIN e ON d.id = e.id` | `d` |
A marker on the other side changes the direction:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id(+) = e.department_id;This is equivalent to:
SELECT d.department_name, e.last_name
FROM departments d
RIGHT OUTER JOIN employees e
ON d.department_id = e.department_id;You can usually rewrite a RIGHT OUTER JOIN as a LEFT OUTER JOIN by reversing the table order. Many teams prefer this because preserving the main table on the left is easier to read.
Convert every optional-table condition
Suppose the old query also limits employees by salary:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;Both conditions reference the optional employees table, so both use (+). The ANSI equivalent places both conditions in ON:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND e.salary > 10000;The salary condition controls which employees match. Departments remain in the result even when no employee earns more than 10,000.
If you omit (+) from the old salary condition, or put the condition in WHERE in the ANSI version, the result changes:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
WHERE e.salary > 10000;For a department without a qualifying employee, e.salary is NULL. The WHERE condition is not true, so that department is removed. The outer join has effectively become an inner join for this filter.
A mechanical conversion method
Use this sequence:
- Find the
(+)marker. - Identify the table without the marker. That table is preserved.
- Write that table first and use
LEFT OUTER JOIN. - Move the equality condition into
ON. - Move every other condition involving the optional table into
ON. - Leave independent filters in
WHERE. - Compare row counts and check rows with no match.
Why ANSI syntax is preferred
The (+) operator has important restrictions. It cannot express a full outer join directly, and legacy outer join conditions become difficult to maintain when several tables and filters are involved. It also cannot be freely combined with conditions using OR or IN. Mixing ANSI joins and (+) in the same query block is not allowed.
ANSI syntax shows the join relationship in JOIN ... ON and separates it from result filtering in WHERE. It also supports FULL OUTER JOIN, USING, and clearer multi-table join chains. For new code, use ANSI syntax. For existing code, convert carefully and test unmatched rows.
Exercise
Rewrite this query using ANSI syntax: SELECT d.departmentname, e.lastname FROM departments d, employees e WHERE d.departmentid = e.departmentid(+) AND e.jobid(+) = 'ITPROG';
Show answer
SELECT d.departmentname, e.lastname FROM departments d LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid AND e.jobid = 'ITPROG';