Sub topic 5
Reading and Writing the (+) Operator
Prerequisites and the basic idea
This chapter assumes that you know SELECT, WHERE, table aliases, primary keys, foreign keys, and NULL.
The (+) operator is Oracle's older syntax for an outer join. It appears in the WHERE clause of a query that uses comma-separated tables. The operator goes next to the columns of the table that may not have a matching row.
Suppose every employee belongs to a department, but some departments have no employees. To keep those empty departments in the result, place (+) on the employee side:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+);Read the condition as follows: match each department to its employees, but allow the employee row to be missing. Oracle returns NULL for e.last_name when no employee matches.
This is equivalent to the clearer ANSI form:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id;The table without (+) is the preserved, or required, table. The table with (+) is the optional table.
Reading the direction of an outer join
The location of (+) determines which table is optional.
| Legacy condition | Preserved table | ANSI equivalent |
|---|---|---|
| `d.id = e.department_id(+)` | `departments d` | `departments d LEFT JOIN employees e` |
| `e.department_id(+) = d.id` | `departments d` | `departments d LEFT JOIN employees e` |
| `e.department_id = d.id(+)` | `employees e` | `employees e LEFT JOIN departments d` |
The operator can appear on either side of the equality. What matters is the table whose column carries (+).
For a right-style result, put (+) on the department side:
SELECT e.last_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id(+);This keeps every employee, including employees whose department is missing.
Adding filters correctly
Every condition involving the optional table must also carry (+) when the condition is meant to be part of the outer join.
This query keeps every department and matches only employees earning more than 10,000:
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 to apply the salary test while matching employees. A department with no employee earning more than 10,000 remains in the result.
Without that marker, the condition becomes an ordinary filter:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary > 10000;Because an unmatched department has e.salary equal to NULL, e.salary > 10000 is not true. The department is removed. In practice, this often makes the query behave like an inner join.
Restrictions and why ANSI syntax is preferred
The (+) syntax is difficult to read and has important restrictions. It belongs in the WHERE clause, cannot be combined with ANSI join syntax in the same query block, and has limitations with expressions such as OR and IN. It also cannot express a full outer join directly.
ANSI syntax separates matching rules in ON from final filtering in WHERE, so the intended behavior is easier to see:
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;When maintaining old code, first identify the table marked with (+), then move its equality into ON. Move other optional-table predicates into the same ON clause. Test the rewritten query against departments or employees that have no match.
Exercise
In FROM departments d, employees e WHERE d.department_id = e.department_id(+) AND e.job_id(+) = 'IT_PROG', which table is preserved, and where should the job condition go in ANSI syntax?
Show answer
departments d is preserved. The ANSI version should put e.job_id = 'IT_PROG' in the ON clause, alongside the department-id condition.