Sub topic 5
Reading and Writing the (+) Operator
Before Oracle 9i added ANSI join syntax, the only way to write an outer join in Oracle was the (+) operator. Huge amounts of production SQL still use it: old reports, views, ETL scripts, and code generated by older tools. This chapter assumes you can write an inner join, know what NULL means in a comparison, and use table aliases. It teaches you to read (+) queries correctly and to write them without silent mistakes.
The core rule
In (+) syntax the tables are listed in the FROM clause, separated by commas. The join conditions go in the WHERE clause. To make a join "outer", you put (+) after a column of the table that may have no matching row.
The marker goes on the side that is allowed to be missing. Oracle fills that table's columns with NULL for any row on the other side that has no partner. The table without the marker is the one whose rows are all kept.
Start with the inner join, which has no marker:
SELECT e.employee_id, e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;Now keep every department, even those with no employees:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);Here employees carries the (+). Departments are preserved, and e.last_name is NULL for a department that has no employees. The ANSI equivalent is:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id;A way to remember it
Many people read (+) as "plus the NULL row". The table with (+) is padded with an imaginary all-NULL row, so unmatched rows from the other table can still pair with something. The marker does not mark the table you are keeping. It marks the table that is being filled in.
Left, right, and the position of the marker
The order of the tables in the FROM list and the order of the two sides of the equals sign do not matter for (+). Only the marker decides which side is preserved. These two queries are identical:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);
SELECT d.department_name, e.last_name
FROM employees e, departments d
WHERE e.department_id(+) = d.department_id;That freedom is why the terms "left" and "right" outer join do not apply to this syntax. In ANSI syntax the position of the table relative to the keyword JOIN decides which side is preserved. In (+) syntax only the marker decides.
| Goal | ANSI form | (+) form |
|---|---|---|
| Keep all departments | `departments d LEFT OUTER JOIN employees e ON ...` | `WHERE d.department_id = e.department_id(+)` |
| Keep all employees | `departments d RIGHT OUTER JOIN employees e ON ...` | `WHERE d.department_id(+) = e.department_id` |
| Keep both sides | `FULL OUTER JOIN` | Not possible in one join |
To keep all employees, including any whose department_id is NULL, put the marker on departments:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+);Filters on the outer table need the marker too
This is the rule that causes most (+) bugs. In ANSI syntax you choose between ON and WHERE for a filter. In (+) syntax everything lives in WHERE, so the marker takes over that decision.
Take this ANSI query. It keeps every department and attaches only employees earning more than 10000:
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 correct (+) version marks the salary condition as well:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;The (+) on e.salary tells Oracle that this condition is part of the join. A department with no employee above 10000 still appears, with a NULL last_name.
Now leave the marker off:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary > 10000;Oracle now treats e.salary > 10000 as an ordinary filter applied after the join. The NULL-padded rows have a NULL salary, and NULL > 10000 is not true, so those rows are removed. Departments without a high earner vanish, and the query behaves like an inner join. It matches the ANSI query that put the filter in WHERE:
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;Sometimes this is what you want. The problem is that the two versions differ by one easy-to-miss (+), and the query gives no error either way.
Conditions that compare to a constant
The same rule applies to constants and to columns of the outer table compared with a literal. Both of these need the marker to stay inside the join:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.job_id(+) = 'IT_PROG';Without (+) on e.job_id, a department with no programmer would drop out of the result.
Reading an unfamiliar (+) query
When you meet a legacy query, work through it in this order:
- List the tables in the
FROMclause. - In
WHERE, find every condition that contains(+). Group them by the table that carries the marker. - For each marked table, find its partner on the other side of the equals sign. That partner is the preserved table.
- Check that every condition that mentions the marked table, apart from filters you meant to apply afterward, has the marker.
- Treat conditions without any marker as ordinary filters on the joined result.
Consider this query:
SELECT d.department_name, e.last_name, l.city
FROM departments d, employees e, locations l
WHERE d.department_id = e.department_id(+)
AND d.location_id = l.location_id;employees is marked, so departments are preserved and may lack employees. departments and locations are joined with no marker, so that link is an inner join. A department with no matching location is dropped, whether or not it has employees.
Writing a (+) query step by step
Suppose you want every employee, with their manager's name, and employees with no manager must still appear. That is a self join, covered in a later chapter, but the marker rule is the same.
- Write the inner join with two aliases for the same table.
- Decide which side may lack a partner. The employee always exists, and the manager may not.
- Put
(+)on the manager alias.
SELECT e.last_name AS employee, m.last_name AS manager
FROM employees e, employees m
WHERE e.manager_id = m.employee_id(+);This is the same as the ANSI query:
SELECT e.last_name AS employee, m.last_name AS manager
FROM employees e
LEFT OUTER JOIN employees m
ON e.manager_id = m.employee_id;Restrictions you will run into
The operator is limited in ways that ANSI syntax is not. A later chapter covers these in full. The ones you meet most often are:
- A condition that contains
(+)cannot be combined with another condition byOR. Oracle raises ORA-01719. - A column marked with
(+)cannot be compared with a subquery. - A table can be outer joined to only one other table in the same query. Oracle raises ORA-01417 when you break this.
- You cannot put the marker on both sides of one condition, so
(+)cannot express a full outer join. (+)applies to columns. It cannot be applied to a whole expression, though an expression may contain marked columns.
Oracle's documentation recommends ANSI syntax for outer joins because of these limits. Use (+) mainly to read and maintain existing code.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Outer join returns fewer rows than expected | A condition on the outer table has no `(+)` |
| Wrong table preserved | Marker placed on the table you wanted to keep instead of the one that may be missing |
| ORA-01719 | `(+)` condition combined with `OR` or an `IN` list |
| ORA-01417 | A table is outer joined to more than one other table |
| ORA-01468 | A predicate references more than one outer-joined table |
Exercise
A query returns every department, but you notice that departments with no employees earning more than 10000 are missing. The query is: SELECT d.departmentname, e.lastname FROM departments d, employees e WHERE d.departmentid = e.departmentid(+) AND e.salary > 10000; What is wrong, and how do you fix it?
Show answer
The condition e.salary > 10000 has no (+), so Oracle applies it after the join as a filter. NULL-padded rows have a NULL salary and are removed, which makes the query behave like an inner join. Change it to e.salary(+) > 10000 so the filter becomes part of the join and departments without a high earner are kept with a NULL last_name.
Exercise
Write a (+) query that lists every employee and the name of their department, including employees whose department_id is NULL. Then write the ANSI equivalent.
Show answer
(+) form: SELECT e.lastname, d.departmentname FROM employees e, departments d WHERE e.departmentid = d.departmentid(+); ANSI form: SELECT e.lastname, d.departmentname FROM employees e LEFT OUTER JOIN departments d ON e.departmentid = d.departmentid; The marker sits on departments because that table may lack a match.