Sub topic 9
What (+) cannot do
This chapter assumes you can already read a (+) outer join and know how ON differs from WHERE in an ANSI outer join. The (+) operator covers simple cases well. Beyond them, Oracle either raises an error or returns a result you did not intend. Knowing these limits explains why Oracle recommends ANSI syntax for outer joins, and it helps you spot the queries that are hardest to migrate.
The examples use the standard HR sample schema (employees, departments, jobs).
The limits at a glance
| Limit | What happens | Typical error |
|---|---|---|
| No full outer join in one join | Marking both sides is rejected | ORA-01468 |
| A table can be outer joined to only one other table | Query is rejected | ORA-01417 |
| No OR or IN with a (+) column | Query is rejected | ORA-01719 |
| No comparison of a (+) column with a subquery | Query is rejected | ORA-01799 |
| Every condition between the two tables needs (+) | Missing marker silently turns the join into an inner join | none |
| No mixing with ANSI joins in one query block | Query is rejected | ORA-25156 |
Error numbers and exact wording can differ slightly between Oracle versions, so check your own release's documentation if one appears.
No full outer join
The (+) operator marks the side of a condition that may lack matching rows. A full outer join needs both sides to be optional. Marking both sides fails:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id(+);Oracle rejects this with ORA-01468: a predicate may reference only one outer-joined table.
With (+), the usual workaround is two queries combined with UNION ALL. The first is a left outer join. The second returns only the rows the first query could not produce, which here are the departments that have no employees:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+)
UNION ALL
SELECT NULL, d.department_name
FROM departments d
WHERE NOT EXISTS (SELECT 1
FROM employees e
WHERE e.department_id = d.department_id);This returns the same rows as the single ANSI statement below, but it reads both tables twice and you must keep the two halves consistent by hand.
SELECT e.last_name, d.department_name
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;A UNION (without ALL) is a tempting shortcut for the first version, but it removes duplicate rows. If two employees share a last name and have the same department, one of them disappears from the result. Use UNION ALL with the NOT EXISTS half.
A table can be outer joined to only one other table
In one query block, a table marked with (+) can be outer joined to at most one other table. Suppose you want every department and every job to appear, with employees attached where a matching pair exists:
SELECT d.department_name, j.job_title, e.last_name
FROM departments d, jobs j, employees e
WHERE e.department_id(+) = d.department_id
AND e.job_id(+) = j.job_id;Here employees is outer joined to both departments and jobs. Oracle raises ORA-01417: a table may be outer joined to at most one other table.
ANSI syntax has no such rule. You can build every department and job pair with a cross join, then attach employees with one combined condition:
SELECT d.department_name, j.job_title, e.last_name
FROM departments d
CROSS JOIN jobs j
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND e.job_id = j.job_id;This returns a grid of all department and job combinations. A pair with no employee shows NULL in last_name.
A chain is different and works fine. In the next query, employees is outer joined to departments, and job_history is outer joined to employees. Each table is outer joined to exactly one other table:
SELECT d.department_name, e.last_name, jh.start_date
FROM departments d, employees e, job_history jh
WHERE d.department_id = e.department_id(+)
AND e.employee_id = jh.employee_id(+);No OR, no IN, no subquery on the optional side
A condition that contains a (+) column cannot be combined with another condition by OR. Consider an attempt to keep departments and match only high earners or employees with commission:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND (e.salary(+) > 10000 OR e.commission_pct(+) > 0.2);This fails with ORA-01719: outer join operator (+) not allowed in operand of OR or IN. The same error appears if you compare a (+) column with an IN list:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.job_id(+) IN ('IT_PROG', 'SA_REP');A (+) column also cannot be compared with a subquery (ORA-01799). ANSI syntax accepts the OR and IN conditions, because the condition sits inside ON, where it only decides which rows match:
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 OR e.commission_pct > 0.2);
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 ('IT_PROG', 'SA_REP');For the subquery case, you can often compute the value in an inline view and join to that, then use the (+) on the view's column.
The silent failure: a missing marker
The restrictions above produce errors. This one does not, which makes it the most dangerous. If two tables are joined and one of the conditions that touches the optional table lacks (+), Oracle treats that condition as an ordinary filter. The NULL-extended rows then fail it and disappear.
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary > 10000;This looks like a left outer join, but it behaves like an inner join. A department with no employee has e.salary equal to NULL, and NULL > 10000 is not true, so the row is dropped. Departments with no high earner vanish. The fix is to mark the column:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;This matches the ANSI query where the salary test sits in ON. The rule is that every condition that references the optional table in the join must carry the marker, including conditions against constants.
No mixing with ANSI joins
A single query block cannot combine (+) with JOIN ... ON. This fails with ORA-25156: old style outer join (+) cannot be used with ANSI joins:
SELECT d.department_name, e.last_name, l.city
FROM departments d
JOIN locations l ON d.location_id = l.location_id,
employees e
WHERE d.department_id = e.department_id(+);Choose one style for the whole query block. If you are editing a legacy statement and want to add one ANSI join, convert the whole block.
Smaller restrictions
- The marker goes on a column. An expression may contain marked columns, but you cannot put the marker on the expression as a whole.
- The marker does not produce an outer join when one table is in the outer query and the other is in a subquery.
- The syntax gives no name to the direction of the join. In ANSI,
LEFTandRIGHTstate which table keeps its rows. In (+), you must work this out from where the marker sits, and a long WHERE clause hides it.
Why this leads to ANSI syntax
Oracle's documentation recommends ANSI syntax for outer joins because (+) has the restrictions listed above. ANSI syntax also separates join conditions in ON from row filters in WHERE, so the two cases in this chapter's silent-failure example cannot be confused. Keep your ability to read (+) for maintaining old code, and write new outer joins in ANSI syntax.
Exercise
A developer writes WHERE d.departmentid = e.departmentid(+) AND e.hire_date(+) > DATE '2005-01-01' OR e.salary(+) > 15000. What happens, and how would you write the intent in ANSI syntax if the intent is "keep all departments, match employees hired after 2005-01-01 or earning more than 15000"?
Show answer
Oracle rejects the query with ORA-01719, because a (+) column appears in an operand of OR. In ANSI syntax: SELECT d.departmentname, e.lastname FROM departments d LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid AND (e.hire_date > DATE '2005-01-01' OR e.salary > 15000);
Exercise
This query is meant to list every department and its employees earning more than 10000: SELECT d.departmentname, e.lastname FROM departments d, employees e WHERE d.departmentid = e.departmentid(+) AND e.salary > 10000; It returns only departments that have a high earner. Why, and what is the one-token fix?
Show answer
The condition e.salary > 10000 has no (+) marker, so Oracle applies it as a normal filter after the join. Rows from departments with no employees have a NULL salary and are removed, which makes the query behave like an inner join. Changing it to e.salary(+) > 10000 keeps those departments with NULL in last_name.
Exercise
Why can you not write a full outer join between employees and departments using only (+), and what is the (+)-style workaround?
Show answer
A full outer join needs both sides to be optional, but (+) on both sides of one condition raises ORA-01468. The workaround is a left outer join written with (+) combined by UNION ALL with a second query that selects the departments that have no employees, using NOT EXISTS. The ANSI FULL OUTER JOIN does this in a single statement.