Sub topic 4
Where the Filter Goes: ON Versus WHERE
This chapter assumes that you know SELECT, WHERE, and ORDER BY, primary and foreign keys, table aliases, and the meaning of NULL. The examples use the employees and departments tables.
The key difference
In an inner join, a condition in ON usually produces the same rows as the same condition in WHERE. Both forms remove rows that do not satisfy the join and filter conditions.
An outer join has a different rule. A left outer join preserves every row from its left table. If no row matches on the right, Oracle adds a row containing NULL values for the right-table columns.
The ON clause controls which rows match during the join. The WHERE clause filters the result after the join has produced those rows.
| Predicate location | Effect in a left outer join |
|---|---|
| `ON` | Keeps the left row, even when no right row satisfies the predicate |
| `WHERE` | Can remove the left row when the right-side value is `NULL` |
Put the condition in ON to keep unmatched rows
Suppose you want every department, but you want to display only employees whose salary is greater than 10000. Put the salary condition in the ON clause:
SELECT d.department_name, e.last_name, e.salary
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
AND e.salary > 10000
ORDER BY d.department_name, e.last_name;Oracle first tries to match employees to each department. An employee with a salary of 10000 or less does not match this join condition. The department still appears, but the employee columns are NULL.
The result can include a department such as Public Relations with no qualifying employee.
Put the condition in WHERE to require a match
Now place the same condition in WHERE:
SELECT d.department_name, e.last_name, e.salary
FROM departments d
LEFT OUTER JOIN employees e
ON e.department_id = d.department_id
WHERE e.salary > 10000
ORDER BY d.department_name, e.last_name;The join still creates a NULL-extended row for a department without a qualifying employee. However, e.salary > 10000 is not true for NULL. It evaluates to unknown, so the WHERE clause removes that row.
This query therefore returns only departments with an employee whose salary is greater than 10000. In practice, it behaves like an inner join for this condition.
A useful decision rule
Ask which rows must always remain:
- Put conditions that define an optional match in
ON. - Put conditions that must be true for the final result in
WHERE. - Put conditions on the preserved table in
WHEREwhen you want to filter that table.
For example, this query keeps every department in location 1700, while showing only highly paid employees:
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
WHERE d.location_id = 1700;The WHERE condition filters departments. The ON condition filters optional employees.
Inner joins
With an inner join, unmatched rows are removed by definition:
SELECT e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
WHERE e.salary > 10000;Moving e.salary > 10000 into the ON clause normally gives the same result:
SELECT e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
AND e.salary > 10000;Using ON for relationships and WHERE for final filters often makes the query easier to read, especially when you later change an inner join to an outer join.
Exercise
You need every department listed, but only employees hired after January 1, 2022 should appear. Should the hire-date condition go in ON or WHERE?
Show answer
Put it in ON. This keeps departments that have no employee matching the date condition and displays NULL employee columns for them.