Sub topic 9
What (+) Cannot Do
Assume you know SELECT, WHERE, table aliases, primary keys, foreign keys, and NULL. The Oracle (+) operator is legacy syntax for an outer join. It can express some left and right joins, but it has restrictions that ANSI join syntax does not have.
The main restrictions
The (+) operator appears in the WHERE clause on the side that may have no matching row:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);This keeps every department. When no employee matches, Oracle supplies NULL for the employee columns.
The restrictions become important when a query has several conditions or several tables.
| Restriction | Effect |
|---|---|
| Cannot use `(+)` with `OR` or `IN` | The query is invalid or cannot express the intended join |
| Must mark every condition involving the optional table | Missing one marker can turn the outer join into an inner join |
| Cannot mix `(+)` and ANSI join syntax in one query block | The query must use one join style for that block |
| Cannot directly express a full outer join | Unmatched rows from both sides require ANSI syntax |
| Has restrictions when one table is outer joined to several tables | Complex relationships are harder to write and maintain |
Oracle versions can differ in some legacy restrictions. ANSI syntax is the portable and clearer choice.
Conditions with `OR` and `IN`
You cannot place (+) in a condition that uses OR or IN:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND (e.job_id = 'SA_REP' OR e.job_id = 'SA_MAN');The intended meaning is usually “keep every department, but match only employees in these jobs.” Write that meaning with an ANSI join instead:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND e.job_id IN ('SA_REP', 'SA_MAN');The condition belongs in ON because it controls which employee rows match. The department rows remain even when no employee satisfies the condition.
Every related condition needs the marker
Suppose you want departments and employees earning more than 10,000. Both conditions involve the optional employees table:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;The second (+) is essential. Without it, Oracle treats e.salary > 10000 like a normal filter. Rows containing the generated NULL employee values fail that filter, so departments without a qualifying employee disappear.
This is the ANSI equivalent:
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;No mixing join styles
Do not combine comma joins with (+) and ANSI joins in the same query block:
SELECT e.last_name, d.department_name, l.city
FROM employees e, departments d
JOIN locations l
ON d.location_id = l.location_id
WHERE e.department_id = d.department_id(+);Rewrite the whole block in ANSI form:
SELECT e.last_name, d.department_name, l.city
FROM employees e
RIGHT OUTER JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id;A consistent join style makes the preserved table and each join condition easier to identify.
Joins that `(+)` cannot express directly
FULL OUTER JOIN preserves unmatched rows from both tables:
SELECT e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;The (+) operator has no single equivalent for this operation. It marks only one side as optional. ANSI syntax also handles multiple joins, explicit cross joins, and join conditions without putting all relationship logic into WHERE.
Exercise
Exercise
Why does this legacy query lose departments with no high-paid employee, and how should it be rewritten?
Show answer
The salary condition lacks (+), so it filters out NULL-extended employee rows. Use LEFT OUTER JOIN and put e.salary > 10000 in the ON clause.