Sub topic 4
Where the filter goes: ON versus WHERE
This chapter assumes you can write basic SELECT statements with WHERE, that you know what a foreign key is, and that you have met LEFT OUTER JOIN in the previous chapter. You should also know that comparing anything to NULL with = or > yields unknown, not true.
In an inner join, you can put a filter in ON or in WHERE and get the same rows. In an outer join, the two placements can give different results. This chapter explains why, shows the effect on sample data, and covers how the same distinction appears in the legacy (+) syntax.
The sample data
All examples in this chapter use these small tables. They are simplified versions of the usual departments and employees tables.
| department_id | department_name |
|---|---|
| 60 | IT |
| 100 | Finance |
| 190 | Payroll |
| employee_id | last_name | department_id | salary |
|---|---|---|---|
| 1 | Hunold | 60 | 9000 |
| 2 | Ernst | 60 | 6000 |
| 3 | Greenberg | 100 | 12008 |
| 4 | Faviet | 100 | 9000 |
Payroll (190) has no employees. Only Greenberg earns more than 10000. Oracle does not guarantee row order without ORDER BY, so the results below may appear in a different order on your system.
Why the placement matters
Conceptually, Oracle processes an outer join in two steps:
- It evaluates the
ONcondition to find matching pairs of rows. - For each row of the preserved table (the side whose rows must all survive) that found no match, it adds one row with
NULLin every column of the other table.
Only after the join is complete does WHERE run. It sees the joined rows, including the NULL-extended ones, and removes any row for which its condition is not true. Oracle's optimizer may reorder the physical work, but the result must match this logical order.
An ON condition therefore decides which rows match. A WHERE condition decides which finished rows are kept. The NULL-extended rows are created between the two steps, so only WHERE can remove them.
Filter in ON: unmatched rows are kept
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;Step 1 pairs each department with employees in that department who earn more than 10000. Only Finance and Greenberg qualify. Step 2 adds a NULL row for IT and Payroll because neither found a match. There is no WHERE, so all three rows survive.
| department_name | last_name |
|---|---|
| IT | NULL |
| Finance | Greenberg |
| Payroll | NULL |
Read this query as: "list every department, and show the high earners in it, if any."
Filter in WHERE: unmatched rows disappear
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;Here the join runs without the salary condition. Step 1 pairs every employee with a department. Step 2 adds Payroll with a NULL employee. Then WHERE evaluates e.salary > 10000 on each row. For Payroll, e.salary is NULL, the comparison is unknown, and the row is removed. Hunold, Ernst and Faviet fail the test as well.
| department_name | last_name |
|---|---|
| Finance | Greenberg |
The query now returns only departments that have a high earner. It behaves like an inner join, and the LEFT OUTER JOIN keyword has no effect.
Choosing by intent
Ask what you want to happen to a preserved row that has no acceptable match.
| You want | Put the condition in |
|---|---|
| Keep the row, show `NULL` for the other table's columns | `ON` |
| Drop the row from the result | `WHERE` |
| Restrict the match between the two tables (the join key) | `ON` |
| Restrict the preserved table's own rows | `WHERE` (see below) |
Conditions on the preserved table
A condition that mentions only the preserved table behaves differently in ON. Compare these two queries:
SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND d.department_name = 'IT';SELECT d.department_name, e.last_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
WHERE d.department_name = 'IT';The first query returns all three departments. A preserved row is never removed by ON, so Finance and Payroll still appear, with NULL employees because they failed the ON condition. Only the IT row shows its employees (Hunold and Ernst). The second query returns only the two IT rows.
If you want to restrict which departments appear, use WHERE. An ON condition on the preserved table does not remove its rows. It only decides which rows get a match, and this is rarely what people intend.
A legitimate WHERE on the outer table: finding non-matches
A WHERE condition on the outer table is correct when you want the rows that have no match. Test a column that cannot be NULL in a real row, such as the primary key:
SELECT d.department_name
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
WHERE e.employee_id IS NULL;Only a NULL-extended row has a NULL employee_id, so the result is Payroll. This pattern is called an anti-join. Do not test a column that may be NULL in real data, such as a commission, because that mixes real rows with extended rows.
Inner joins: no difference
For an inner join, step 2 does not exist, so no row is ever added after matching. These two queries return the same rows:
SELECT d.department_name, e.last_name
FROM departments d
INNER JOIN employees e
ON e.department_id = d.department_id
AND e.salary > 10000;SELECT d.department_name, e.last_name
FROM departments d
INNER JOIN employees e
ON e.department_id = d.department_id
WHERE e.salary > 10000;Both return Finance and Greenberg. Many teams put join keys in ON and other filters in WHERE for inner joins, because it separates "how the tables relate" from "which rows I want." Oracle's optimizer treats the two forms alike. Keep that habit, but remember that it does not carry over to outer joins.
The same distinction in (+) syntax
In the comma syntax, there is no ON. Every condition lives in WHERE, and the (+) marker is what tells Oracle that a condition belongs to the outer join. A condition with (+) on the outer table's column acts like an ON condition. A condition without it acts like a WHERE condition.
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary(+) > 10000;This returns the same three rows as the ON version above. Because e.salary(+) carries the marker, it takes part in matching, and the NULL-extended rows are kept. Now remove the marker from the salary line:
SELECT d.department_name, e.last_name
FROM departments d, employees e
WHERE d.department_id = e.department_id(+)
AND e.salary > 10000;Without (+), the condition filters after the join. It returns only Finance and Greenberg, the same result as the WHERE version. The mapping is mechanical:
| ANSI form | (+) form |
|---|---|
| Condition on the outer table in `ON` | Condition on the outer table's column **with** `(+)` |
| Condition on the outer table in `WHERE` | Condition on the outer table's column **without** `(+)` |
A constant comparison such as e.salary(+) > 10000 is allowed, but (+) has restrictions elsewhere. For example, a condition containing (+) cannot be combined with OR or used with IN on a list of values. Oracle raises an error (ORA-01719) in these cases. The restrictions are covered in a later chapter.
RIGHT and FULL joins
The rule is symmetric. In a RIGHT OUTER JOIN, the right table is preserved, so conditions on the left table behave as the outer table's conditions do above. In a FULL OUTER JOIN, both sides are preserved. A WHERE condition on either table's columns can remove NULL-extended rows from the other side, so a WHERE filter on a full join often turns it into a left, right or inner join. If you need to filter a full join without losing unmatched rows, either put the condition in ON (which restricts matching only) or filter each table in an inline view before joining.
Working through a query
When an outer join returns fewer rows than expected, check these in order:
- Identify the preserved table. It is the left table for
LEFT, the right table forRIGHT. - List every condition that references a column of the other table.
- For each one, decide whether unmatched preserved rows should survive. If they should, make sure the condition is in
ON(or has(+)). - Check for
WHEREconditions that test a nullable column with=,>orLIKE. AddOR column IS NULLonly if you really want to keep the extended rows. Otherwise move the condition toON.
Exercise
Using the sample data, a developer wants every department listed, with the names of employees who earn less than 7000 (Ernst only). They write: SELECT d.departmentname, e.lastname FROM departments d LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid WHERE e.salary < 7000. The query returns one row. What does it return, why is it wrong, and how do you fix it?
Show answer
It returns only IT with Ernst. Finance and Payroll are removed because after the join their rows either fail the salary test (Greenberg, Faviet) or have a NULL salary (Payroll), so WHERE drops them. Move the condition into ON: LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid AND e.salary < 7000. The result is then three rows: IT with Ernst, Finance with NULL, and Payroll with NULL. In (+) syntax, the equivalent is WHERE d.departmentid = e.departmentid(+) AND e.salary(+) < 7000.
Exercise
Write a query that lists only the departments that have no employee earning more than 10000, including departments with no employees at all. Use ANSI syntax.
Show answer
SELECT d.departmentname FROM departments d LEFT OUTER JOIN employees e ON e.departmentid = d.departmentid AND e.salary > 10000 WHERE e.employeeid IS NULL; The salary condition goes in ON so that only high earners match. Departments with no high earner get a NULL-extended row, and WHERE e.employee_id IS NULL keeps exactly those rows. The result is IT and Payroll.