Left outer join vs. right outer join
Compare a left outer join vs. a right outer join with runnable PostgreSQL examples, expected rows, safe rewrites, and checks for filters and repeated matches.

The difference between a left outer join and a right outer join is which table keeps every row: a left outer join preserves the left table, while a right outer join preserves the right table. Both return matching combinations and fill columns from an unmatched side with NULL, SQL's marker for a missing or unknown value. You can rewrite a right outer join as a left outer join by swapping the table positions while keeping the matching condition and explicit output columns unchanged.
The preserved-table rule
The preserved table is the table whose rows survive the join even when no matching row exists in the other table. Left and right refer to the table positions in the query, not to their names, their size, or the order of the output columns.
Start with the reporting requirement. If you need every customer, including customers without orders, preserve customers. If you need every order, including orders without a matching customer record, preserve orders.
- LEFT OUTER JOIN preserves the table before the join operator.
- RIGHT OUTER JOIN preserves the table after the join operator.
- OUTER is optional: LEFT JOIN and LEFT OUTER JOIN mean the same thing, as do RIGHT JOIN and RIGHT OUTER JOIN.
- Neither direction preserves unmatched rows from both tables. A full outer join serves that different purpose.
Two joins with different results
Keeping the table order but changing the join direction changes which unmatched row survives. Run the following exercise in PostgreSQL. Each statement defines its own fictional data through common table expressions, temporary named query results introduced by WITH, so you don't need to create permanent tables.
VALUES supplies the starting rows. The short names c and o are table aliases, which let you identify the source of each selected column. Run the statements in order, making the changes described before each query.
-- Fictional exercise using PostgreSQL.
-- Goal: preserve customers, then preserve orders, then rewrite
-- the right join as a left join with identical output.
-- Starting customers: IDs 1 and 2.
-- Starting orders: 101 references customer 1;
-- 102 references customer 3, absent from the customer list.
-- Match rule: customers.id = orders.customer_id.
-- Calculation for each query: one matching combination
-- + one unmatched preserved row = two output rows.
-- Run 1: preserve customers.
WITH customers(id) AS (VALUES (1), (2)),
orders(id, customer_id) AS (VALUES (101, 1), (102, 3))
SELECT c.id AS customer_id, o.id AS order_id
FROM customers AS c
LEFT OUTER JOIN orders AS o ON c.id = o.customer_id
ORDER BY c.id;
-- Expected output; NULL denotes a missing value:
-- customer_id | order_id
-- 1 | 101
-- 2 | NULL
-- Run 2: change LEFT OUTER JOIN to RIGHT OUTER JOIN.
-- Also change ORDER BY c.id to ORDER BY o.id.
-- Keep table order, selected columns, and match rule unchanged.
WITH customers(id) AS (VALUES (1), (2)),
orders(id, customer_id) AS (VALUES (101, 1), (102, 3))
SELECT c.id AS customer_id, o.id AS order_id
FROM customers AS c
RIGHT OUTER JOIN orders AS o ON c.id = o.customer_id
ORDER BY o.id;
-- Expected output:
-- customer_id | order_id
-- 1 | 101
-- NULL | 102
-- Run 3: replace Run 2's FROM and JOIN clauses.
-- Put orders first, customers second, and use LEFT OUTER JOIN.
-- Keep selected columns, aliases, match rule, and ordering.
WITH customers(id) AS (VALUES (1), (2)),
orders(id, customer_id) AS (VALUES (101, 1), (102, 3))
SELECT c.id AS customer_id, o.id AS order_id
FROM orders AS o
LEFT OUTER JOIN customers AS c ON c.id = o.customer_id
ORDER BY o.id;
-- Expected output, identical to Run 2:
-- customer_id | order_id
-- 1 | 101
-- NULL | 102
Customer 2 survives only Run 1 because customers is the preserved table in that query. Order 102 survives Runs 2 and 3 because both preserve orders. The absent customer reference is deliberate exercise data, not a recommendation for database design.
Notice that the selected customer ID comes from customers, not orders. Order 102 stores a customer reference, but its selected customer ID is NULL because no customer row matched.
Try predicting the unmatched row before running another query in LEFT JOIN: keep rows without a match.
A safe direction change
A safe right-to-left rewrite swaps the two table operands and changes the join direction together. Run 3 demonstrates the rewrite: orders moves first, customers moves second, and the result still preserves every order.
Changing only RIGHT to LEFT in Run 2 would preserve customers instead, producing Run 1's rows. Swapping the table positions is essential. Keep the aliases attached to their original tables, the matching condition unchanged, and the selected columns in their original order.
Explicit output columns make the comparison safer. Selecting every column can change output column order when you swap table positions. The simple two-table rewrite also isn't permission to rearrange a longer chain of outer joins, where one join's result becomes another join's input.
Use a right join when keeping the existing table order makes the requirement clearer. Put the preserved table first and use a left join when that arrangement is easier to read. Neither direction is inherently faster; performance depends on the database, data, and execution plan, the database's chosen method for running the query.
When you're ready to check whether a rewrite preserves its result, try Performance rewrites with the same meaning.
Filters and repeated matches
Filters can remove rows that an outer join preserved, and multiple matches can produce several output rows for one preserved record. Those effects are separate from join direction.
ON controls which rows match. WHERE filters the joined result. To see the difference, first restrict matching to order 101 in ON. Then move that restriction to WHERE exactly as shown.
-- Fictional PostgreSQL filter exercise.
-- Goal: compare a matching restriction with a result filter.
-- Starting customers: 1 and 2.
-- Starting orders: (101, 1) and (102, 3).
-- Base match: c.id = o.customer_id.
-- Restriction: order ID must equal 101.
-- Run 1: restrict matching in ON; keep every customer.
WITH customers(id) AS (VALUES (1), (2)),
orders(id, customer_id) AS (VALUES (101, 1), (102, 3))
SELECT c.id AS customer_id, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o
ON c.id = o.customer_id AND o.id = 101
ORDER BY c.id;
-- Calculation: one match + one unmatched customer = two rows.
-- customer_id | order_id
-- 1 | 101
-- 2 | NULL
-- Run 2: remove AND o.id = 101 from ON.
-- Add WHERE o.id = 101 before ORDER BY.
WITH customers(id) AS (VALUES (1), (2)),
orders(id, customer_id) AS (VALUES (101, 1), (102, 3))
SELECT c.id AS customer_id, o.id AS order_id
FROM customers AS c
LEFT JOIN orders AS o ON c.id = o.customer_id
WHERE o.id = 101
ORDER BY c.id;
-- Calculation: two joined rows - one rejected row = one row.
-- customer_id | order_id
-- 1 | 101
The WHERE comparison doesn't evaluate as true for customer 2's NULL order ID, so the filter removes that row. Moving a condition between ON and WHERE changes its purpose; the clauses aren't interchangeable.
Preserving customers also doesn't guarantee one row per customer. A customer with several matching orders appears once for each matching order. Those repeated customer values aren't necessarily erroneous duplicates. Check whether the intended output represents customers or customer-order combinations before removing rows.
The tiny exercises isolate preservation and filtering. They don't measure performance or demonstrate every multiple-match case, so use additional data before treating a rewrite as ready for workplace use.
Practice methods and selection criteria
Choose a learning method by whether you need an explanation, executable results, or feedback that identifies a mistake. Reading a correct query isn't the same as predicting its unmatched rows before execution.
| Approach | Best use | Result verification | Mistake diagnosis | Setup and remaining work |
|---|---|---|---|---|
| Hands-on practice with feedback | Predicting preserved rows and correcting queries | An exercise check evaluates your work | Feedback can identify the incorrect step | Choose browser-based practice to avoid local installation; still explain the result yourself |
| Local PostgreSQL scratch database | Changing data and testing rewrites freely | Run queries and compare explicit outputs | You must investigate unexpected rows | Requires a PostgreSQL environment; create your own test cases |
| Video courses | Following a visual explanation of table preservation | Pause and run the demonstrated query yourself | An explanation may clarify the rule, but doesn't check your query | Viewing needs little setup; executable practice needs a query environment |
| Books or free documentation | Checking syntax and reviewing join rules | Adapt examples and execute them | Use the explanation to trace your own error | Reading needs little setup; you must design and verify variations |
The Work Gym's SQL course offers browser-based work in working copies of tools, automatic exercise checks, and a hint that names the mistake. Use checked practice when you can write the syntax but struggle to identify why the returned rows differ from your prediction.
Apply the same preserved-table decision to another example in Practice: more joins.
- Name the table whose records must all survive the join.
- Predict which unmatched records should appear.
- Run the query and compare explicit columns and row values.
- For a rewrite, compare repeated rows as well as distinct values.
- Investigate WHERE filters and multiple matches before changing join direction.
Common questions
What is the point of a right join? A right join preserves every row from the table on the right, including rows without a match on the left. It lets you express that requirement without reversing the table order in an existing two-table query.
What does a left outer join return? A left outer join returns every matching combination plus each unmatched row from the left table. Columns from the right table are NULL for unmatched left rows, and several right-side matches can produce several rows for one left-side record.
Is LEFT JOIN the same as LEFT OUTER JOIN? Yes, LEFT JOIN and LEFT OUTER JOIN mean the same thing in PostgreSQL. OUTER is optional and doesn't change which rows survive.
What's the difference between LEFT JOIN and plain JOIN? In PostgreSQL, plain JOIN means INNER JOIN, which returns only matching combinations. LEFT JOIN also returns unmatched left-side rows, with NULL in columns from the right side.
Practice what this post teaches
Can a right outer join always be rewritten as a left outer join? An individual right outer join can be expressed as a left outer join by swapping its two operands and preserving the matching condition and explicit output columns. That equivalence doesn't mean you can freely reorder a longer chain of outer joins, and selecting every column can change output column order after the swap.