Sub topic 6
Converting (+) Queries to ANSI Joins
This chapter assumes you can read basic inner and outer joins, know the difference between a condition in ON and one in WHERE, and have seen the (+) operator before. The examples use the Oracle HR sample schema (employees, departments, locations).
You will convert legacy queries mechanically, using a short set of rules. You will also learn to spot the places where a conversion changes the meaning, and to prove that the new query returns the same rows.
Why convert
Oracle still accepts (+), but the ANSI syntax has fewer restrictions and states the join type in words. It also separates join conditions (ON) from row filters (WHERE). Only ANSI syntax supports FULL OUTER JOIN directly. Most migrations are done to make queries readable and to remove the limits covered in the previous chapter.
The core rule
In a legacy query, every WHERE predicate is one of two kinds:
- A predicate with
(+)on a column of table T. It is part of the outer join to T and belongs in T'sONclause. - A predicate without
(+). It filters the final result and belongs inWHERE.
The table carrying the (+) is the one that may be missing rows. In ANSI terms it is the table on the optional side of the join.
-- Legacy
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);employees carries the (+), so departments is the preserved side:
-- ANSI
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id;The marker can sit on either side of the equals sign, so e.department_id(+) = d.department_id means the same thing. Only the table that carries the marker matters.
Step-by-step procedure
Step 1: filters on the optional table
The legacy query below keeps every department. It matches only employees who earn more than 10000.
-- Legacy
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;Both predicates carry the marker on e, so both go into ON:
-- ANSI
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
AND e.salary > 10000;Step 2: a forgotten marker
Suppose the legacy author left the (+) off the salary condition:
-- Legacy: salary condition has no (+)
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary > 10000;The unmarked predicate is a filter. For a department with no match, e.salary is NULL, so NULL > 10000 is not true and the row is discarded. The query returns only departments that have a high earner. The faithful ANSI translation keeps the predicate in WHERE:
-- ANSI, faithful translation
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
WHERE e.salary > 10000;The best rewrite uses the join type that matches the behavior:
-- ANSI, clearer intent
SELECT d.department_name, e.last_name
FROM departments d
INNER JOIN employees e
ON d.department_id = e.department_id
WHERE e.salary > 10000;Before you choose, ask the query's owner which behavior was intended. A missing marker is a common bug in legacy code, and a mechanical conversion keeps the bug.
Step 3: filters on the preserved table
A predicate on the preserved table has no marker, and it stays in WHERE:
-- Legacy
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND d.location_id = 1700;-- ANSI
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
WHERE d.location_id = 1700;Putting d.location_id = 1700 in ON would not remove any departments. A left join always keeps every row of the preserved table. The department rows that fail the filter would appear with NULL employee columns.
Summary of the mapping
| Legacy form | ANSI form |
|---|---|
| `a.x = b.x(+)` | `a LEFT OUTER JOIN b ON a.x = b.x` |
| `a.x(+) = b.x` | `a RIGHT OUTER JOIN b ON a.x = b.x`, or `b LEFT OUTER JOIN a ON ...` |
| `b.col(+) > 10` | extra condition in the `ON` clause that joins `b` |
| `b.col(+) = 'Y'` (a constant) | extra condition in the same `ON` clause |
| `a.col = 10`, no marker | `WHERE a.col = 10` |
| `b.col > 10`, no marker | `WHERE b.col > 10`. Consider `INNER JOIN` instead. |
Chains of outer joins
With three tables, the order of the FROM clause matters in ANSI syntax. Each JOIN links a new table to tables that already appear to its left.
-- Legacy: employees without a department are kept,
-- and departments without a location are kept
SELECT e.last_name, d.department_name, l.city
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id(+)
AND d.location_id = l.location_id(+);-- ANSI
SELECT e.last_name, d.department_name, l.city
FROM employees e
LEFT OUTER JOIN departments d ON e.department_id = d.department_id
LEFT OUTER JOIN locations l ON d.location_id = l.location_id;Write the chain in the direction the data flows: employees, then departments, then locations. Here l links to d, so d must appear first.
Now remove the second marker:
-- Legacy: second predicate has no (+)
SELECT e.last_name, d.department_name, l.city
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id(+)
AND d.location_id = l.location_id;An employee with no department gets a NULL d.location_id. That row cannot match any location, and the unmarked predicate removes it. The outer join to departments has no effect. The equivalent ANSI form uses JOIN for the second link:
SELECT e.last_name, d.department_name, l.city
FROM employees e
LEFT OUTER JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id;An inner join after a left join discards the NULL-extended rows in the same way. A mixed-marker query like this is often a mistake, so confirm what the author wanted.
Rewriting emulated full outer joins
Because (+) cannot produce a full outer join, legacy code often uses UNION:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+)
UNION
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id;This becomes one statement:
SELECT e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;UNION removes duplicate rows, and FULL OUTER JOIN does not. If two employees share a last name and department, the old query returns one row and the new one returns two. Decide which behavior is correct. If the old behavior is needed, add DISTINCT to the new query.
Restrictions that disappear
Some legacy queries cannot be written with (+), so conversion often also fixes workarounds. These legacy errors have no ANSI equivalent, because ANSI syntax lacks the restrictions behind them:
| Error | Legacy cause |
|---|---|
| ORA-01719 | `(+)` inside an `OR` or an `IN` condition |
| ORA-01417 | A table outer joined to more than one other table |
| ORA-01468 | A predicate that references more than one outer-joined table |
Verifying the conversion
Do not trust a rewrite by reading it. Run both versions and compare. MINUS finds rows that exist in one result and not the other. Run it in both directions, and expect no rows each time:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000
MINUS
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
AND e.salary > 10000;Swap the two halves and run it again. Then compare row counts, because MINUS ignores duplicates:
SELECT COUNT(*)
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;
SELECT COUNT(*)
FROM departments d
LEFT OUTER JOIN employees e
ON d.department_id = e.department_id
AND e.salary > 10000;Common conversion mistakes
- Moving every predicate to
ON. Predicates without a marker on the preserved table must stay inWHERE. - Moving every predicate to
WHERE. Marked predicates on the optional table inWHEREturn the outer join into an inner join. - Reversing the join direction. The table with the
(+)becomes the optional side, so it is the right table of aLEFT OUTER JOIN. - Reordering tables carelessly. Each
ONclause may reference only tables that appear earlier in theFROMclause. - Ignoring duplicates from
UNION. Replacing an emulated full join can change the row count.
Exercise
Convert this legacy query to ANSI syntax. Say which predicates go in ON and which in WHERE, and which join type you use. SELECT d.departmentname, e.lastname, l.city FROM departments d, employees e, locations l WHERE d.departmentid = e.departmentid(+) AND e.hiredate(+) > DATE '2005-01-01' AND d.locationid = l.locationid AND l.countryid = 'US';
Show answer
The marked predicates (d.department_id = e.department_id(+) and e.hire_date(+) > DATE '2005-01-01') go in the ON clause of a LEFT OUTER JOIN to employees. The link from departments to locations has no marker, so it becomes an inner JOIN. The country filter has no marker and stays in WHERE.
SELECT d.departmentname, e.lastname, l.city
FROM departments d
LEFT OUTER JOIN employees e
ON d.departmentid = e.departmentid
AND e.hiredate > DATE '2005-01-01'
JOIN locations l
ON d.locationid = l.locationid
WHERE l.countryid = 'US';
Exercise
A legacy query contains WHERE d.department_id = e.department_id(+) AND e.salary > 10000. After conversion to a LEFT OUTER JOIN with the salary condition in WHERE, a colleague asks why the outer join is pointless. What do you answer, and what do you change?
Show answer
For departments with no matching employee, e.salary is NULL, and NULL > 10000 is not true, so those rows are removed by the WHERE clause. The result contains only departments that have a high earner, which is an inner join result. Replace the LEFT OUTER JOIN with INNER JOIN, or, if the author wanted to keep all departments, move the salary condition into ON.