Multi-Table Joins and FULL OUTER JOIN
Multiple joins apply from left to right. The result of the previous join becomes one table, and the next join attaches to that result. With only INNER JOIN, changing the order gives the same result; once an outer join is mixed in, order and parentheses change the result.
| Combination | Changing the table order |
|---|---|
| INNER JOIN only | Gives the same result. |
| LEFT JOIN mixed in | Changes which table's rows are preserved. |
NOTE The order here is the logical order that defines the result. The order in which the DB actually reads the tables is chosen separately by the optimizer, within what gives the same result (Driving set and join order, Direction of an outer join).
Customers per grade: keeping unmatched grades
Customers 101-130 include no VIP ('최우수') customers. With grade_cd on the left and a LEFT JOIN, the VIP grade also remains, with 0.
Customers per grade (customers 101-130)
SELECT g.grade_nm, COUNT(c.cust_id) AS customers
FROM grade_cd g
LEFT JOIN customers c
ON c.grade = g.grade
AND c.cust_id BETWEEN 101 AND 130
GROUP BY g.grade_nm
ORDER BY customers DESC
The result is NORMAL ('일반') 27, GOLD ('우수') 3, VIP ('최우수') 0.
Pitfall: INNER JOIN after LEFT JOIN
Now attach canceled orders. Joining the third table with JOIN (INNER) makes the VIP row disappear.
VIP grade disappearing
SELECT g.grade_nm, COUNT(DISTINCT c.cust_id) AS customers, COUNT(o.ord_id) AS canceled
FROM grade_cd g
LEFT JOIN customers c
ON c.grade = g.grade
AND c.cust_id BETWEEN 101 AND 130
JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED'
GROUP BY g.grade_nm
ORDER BY customers DESC
- In the result of
grade_cd LEFT JOIN customers, the VIP row has a NULLc.cust_id. JOIN orders ON o.cust_id = c.cust_idis applied to that result.=with NULL is unknown, so the VIP row has no match and is discarded.
When the INNER JOIN condition references a column of the NULL-filled side (c), the preceding LEFT JOIN becomes equivalent to an INNER JOIN. This is the same principle as writing a right-side condition in WHERE. Customers with no canceled orders are dropped as well.
Fix 1: make the later join a LEFT JOIN too
Following join also a LEFT JOIN
SELECT g.grade_nm, COUNT(DISTINCT c.cust_id) AS customers, COUNT(o.ord_id) AS canceled
FROM grade_cd g
LEFT JOIN customers c
ON c.grade = g.grade
AND c.cust_id BETWEEN 101 AND 130
LEFT JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED'
GROUP BY g.grade_nm
ORDER BY customers DESC
VIP comes back with 0, 0. Customers with no canceled orders also remain, so customers is 27, 3, 0, as in the first query.
Fix 2: group first with parentheses
To count only "customers with canceled orders" while keeping all grades, group the INNER JOIN of customers and orders first with parentheses, and LEFT JOIN that result.
Grouping customers and orders first
SELECT g.grade_nm, COUNT(DISTINCT c.cust_id) AS customers, COUNT(o.ord_id) AS canceled
FROM grade_cd g
LEFT JOIN (customers c
JOIN orders o
ON o.cust_id = c.cust_id
AND o.status = 'CANCELED')
ON c.grade = g.grade
AND c.cust_id BETWEEN 101 AND 130
GROUP BY g.grade_nm
ORDER BY customers DESC
All 3 grade rows remain, and customers counts only customers with canceled orders. The two fixes mean different things.
| Form | Grades | Customers |
|---|---|---|
LEFT JOIN c … LEFT JOIN o | All | All customers in range |
LEFT JOIN (c JOIN o) | All | Only customers with canceled orders |
TIP A form with the preserved table at the far left followed only by
LEFT JOINs is easy to read. To give the parenthesized join its own name, pull it out into an inline view or WITH.
FULL OUTER JOIN: keeping both sides
FULL [OUTER] JOIN keeps all rows of both tables. Matched rows merge into one row; unmatched rows get NULLs in the other side's columns. Use it to see the difference between two lists at once.
Compare a list of grade codes (VALUES) with grade_cd.
Comparing two code lists
SELECT COALESCE(v.grade, g.grade) AS grade,
v.grade AS in_list, g.grade AS in_grade_cd, g.grade_nm
FROM (VALUES ('VIP'), ('GOLD'), ('SILVER')) AS v(grade)
FULL JOIN grade_cd g ON g.grade = v.grade
ORDER BY grade
| Row | in_list | in_grade_cd |
|---|---|---|
| GOLD, VIP | Value | Value |
| NORMAL | NULL | Value |
| SILVER | Value | NULL |
Since it is not known which side the key came from, merge them with COALESCE(v.grade, g.grade) into a single key column. ORDER BY and joins in an outer query also use this merged key.
Merging two aggregates by key
Aggregate canceled and paid (PAID) counts per customer separately, then merge them by customer ID. A customer present on only one side has NULL for the other side's value, so COALESCE fills in 0.
Canceled and paid counts per customer (customers 101-130)
SELECT COALESCE(x.cust_id, y.cust_id) AS cust_id,
COALESCE(x.cnt, 0) AS canceled,
COALESCE(y.cnt, 0) AS paid
FROM (SELECT cust_id, COUNT(*) AS cnt FROM orders
WHERE cust_id BETWEEN 101 AND 130 AND status = 'CANCELED'
GROUP BY cust_id) x
FULL JOIN
(SELECT cust_id, COUNT(*) AS cnt FROM orders
WHERE cust_id BETWEEN 101 AND 130 AND status = 'PAID'
GROUP BY cust_id) y
ON y.cust_id = x.cust_id
ORDER BY cust_id
Each customer present on either side appears as one row. Customers present on neither side do not appear. To keep every customer, put customers at the far left and LEFT JOIN both aggregates.
NOTE A condition that filters one side's column after a
FULL JOIN, such asWHERE x.cust_id IS NOT NULL, discards rows without a match on that side and turns it into aLEFT JOIN. Oracle's(+)syntax cannot be used on both sides, so it cannot express aFULL JOIN.