Outer Joins and Condition Placement
INNER JOIN discards rows without a match. An OUTER JOIN keeps all rows of one table even without a match, and fills the columns of the unmatched side with NULLs.
| Join | Rows kept |
|---|---|
LEFT [OUTER] JOIN | All rows of the left table |
RIGHT [OUTER] JOIN | All rows of the right table |
FULL [OUTER] JOIN | All rows of both tables |
RIGHT JOIN equals a LEFT JOIN with the table order swapped, so queries usually use LEFT JOIN throughout. FULL JOIN and joins of three or more tables are covered in Multi-table joins and FULL OUTER JOIN.
LEFT JOIN
Attach the canceled orders of customers 101-110. Customers with no canceled orders still keep one row each, with NULL order columns.
Customers and canceled orders
SELECT c.cust_id, o.ord_id, o.status, o.amt
FROM customers c
LEFT JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED'
WHERE c.cust_id BETWEEN 101 AND 110
ORDER BY c.cust_id, o.ord_id
Moving the condition to WHERE
Move the same condition o.status = 'CANCELED' to WHERE.
Condition written in WHERE
SELECT c.cust_id, o.ord_id, o.status, o.amt
FROM customers c
LEFT JOIN orders o
ON o.cust_id = c.cust_id
WHERE c.cust_id BETWEEN 101 AND 110
AND o.status = 'CANCELED'
ORDER BY c.cust_id, o.ord_id
Customers with no canceled orders are gone. The join runs first and WHERE runs after it (logical order of evaluation).
- The
ONcondition pairs rows. A customer without a match remains as a row with NULL order columns. WHEREfilters that result. For a NULLo.status,= 'CANCELED'is unknown, so the row is dropped.
As a result, the outer join becomes an INNER JOIN. The rule is:
| Condition | Where it goes |
|---|---|
| Condition that filters the right table (the side filled with NULLs) | ON |
| Condition that filters the whole result, conditions on the left table | WHERE |
Only unmatched rows: anti-join
Filtering with WHERE right.key IS NULL after an outer join leaves only unmatched rows. This gives "customers with no canceled orders at all".
Customers with no canceled orders
SELECT c.cust_id, c.cust_nm
FROM customers c
LEFT JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED'
WHERE c.cust_id BETWEEN 101 AND 120
AND o.ord_id IS NULL
ORDER BY c.cust_id
The same meaning can be written with NOT EXISTS (Subqueries). The column tested with IS NULL must be one that cannot be NULL in the right table (such as the primary key).
Outer joins and aggregation
COUNT(*) after an outer join counts an unmatched customer as 1. Counting a column of the right table skips the NULL and gives 0.
Canceled orders per customer
SELECT c.cust_id,
COUNT(*) AS count_star,
COUNT(o.ord_id) AS canceled_orders
FROM customers c
LEFT JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED'
WHERE c.cust_id BETWEEN 101 AND 110
GROUP BY c.cust_id
ORDER BY c.cust_id
NOTE Oracle has an old outer join syntax written as
WHERE o.cust_id(+) = c.cust_id. It gives the same result as the standardLEFT JOIN, but it cannot expressFULLjoins and some conditions, so new SQL uses the standard syntax.