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.

JoinRows kept
LEFT [OUTER] JOINAll rows of the left table
RIGHT [OUTER] JOINAll rows of the right table
FULL [OUTER] JOINAll 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).

  1. The ON condition pairs rows. A customer without a match remains as a row with NULL order columns.
  2. WHERE filters that result. For a NULL o.status, = 'CANCELED' is unknown, so the row is dropped.

As a result, the outer join becomes an INNER JOIN. The rule is:

ConditionWhere it goes
Condition that filters the right table (the side filled with NULLs)ON
Condition that filters the whole result, conditions on the left tableWHERE

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 standard LEFT JOIN, but it cannot express FULL joins and some conditions, so new SQL uses the standard syntax.