Sub topic 5
RIGHT OUTER JOIN and Why It Mirrors LEFT
Prerequisites
This chapter assumes that you know SELECT, FROM, and WHERE, understand NULL, and can write an INNER JOIN with an ON condition. It also assumes that you understand primary keys and foreign keys.
A RIGHT OUTER JOIN, usually written as RIGHT JOIN, keeps every row from the table on the right side of the join. Matching rows from the left table are added when they exist. If no match exists, columns from the left table contain NULL.
Assume these tables:
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
total DECIMAL(10, 2) NOT NULL
);The customers are Ada, Ben, and Cleo. Orders 101, 102, and 103 belong to Ada or Ben. Order 104 has no customer.
How RIGHT JOIN Preserves the Right Table
In this query, orders is the right table:
SELECT c.name, o.order_id, o.total
FROM customers c
RIGHT JOIN orders o
ON o.customer_id = c.customer_id;Every order appears, including order 104. Because order 104 has no matching customer, its customer columns are NULL.
| name | order_id | total |
|---|---|---|
| Ada | 101 | 50.00 |
| Ada | 102 | 20.00 |
| Ben | 103 | 35.00 |
| NULL | 104 | 15.00 |
An inner join would discard order 104. The right join preserves it because the right table is orders.
Why RIGHT JOIN Mirrors LEFT JOIN
A RIGHT JOIN and a LEFT JOIN can express the same relationship. You only need to reverse the table order:
-- RIGHT JOIN
SELECT c.name, o.order_id
FROM customers c
RIGHT JOIN orders o
ON o.customer_id = c.customer_id;
-- Equivalent LEFT JOIN
SELECT c.name, o.order_id
FROM orders o
LEFT JOIN customers c
ON o.customer_id = c.customer_id;Both queries preserve every order and add customer information when available.
The choice is mainly about which table you want to write first. Many teams prefer LEFT JOIN because it keeps the primary or preserved table at the start of the query. For example, this version reads naturally as “show every order, with its customer if known”:
SELECT o.order_id, c.name
FROM orders o
LEFT JOIN customers c
ON c.customer_id = o.customer_id;The equivalent right join is valid but often less familiar:
SELECT o.order_id, c.name
FROM customers c
RIGHT JOIN orders o
ON c.customer_id = o.customer_id;Use the form that makes the preserved table easiest to identify. Consistency matters more than the keyword.
Predicting NULL-Padded Rows
To predict a right join, use this process:
- Start with every row from the right table.
- Find matching rows in the left table.
- Add one result row for each match.
- For an unmatched right-side row, put
NULLin every selected left-table column.
The same multiplication rule applies as with other joins. If two left-side rows match one right-side row, that right-side row appears twice. A join preserves rows, but it does not guarantee one result row per input row.
The direction of preservation also matters when chaining joins. A later WHERE condition on a nullable table can remove the NULL-padded rows and make an outer join behave like an inner join.
RIGHT JOIN or Rewritten LEFT JOIN?
Use a RIGHT JOIN when the query is already organized around a right-side table or when it makes a complex join chain clearer. Otherwise, rewrite it as a LEFT JOIN by swapping the table order.
Do not confuse the join direction with the foreign-key direction. A foreign key describes a data relationship. It does not decide which table must be preserved.
Exercise
Rewrite this query as an equivalent LEFT JOIN: SELECT c.name, o.orderid FROM customers c RIGHT JOIN orders o ON o.customerid = c.customer_id;
Show answer
SELECT c.name, o.orderid FROM orders o LEFT JOIN customers c ON o.customerid = c.customer_id;