Left, Right and Inner Joins

Left Outer Join vs Right Outer Join in SQL

A left outer join keeps every row from the first table, and a right outer join keeps every row from the second. Examples of both, and when to use each.

On this page
  1. The short answer
  2. Sample tables used in this article
  3. How a LEFT OUTER JOIN works
  4. How a RIGHT OUTER JOIN works
  5. Swapping the tables gives the same rows
  6. Where the NULLs come from
  7. Filters belong in ON when the table is optional
  8. Finding rows with no match
  9. Joining more than two tables
  10. Which one should you use?
  11. Support across databases
  12. Summary

The short answer

A LEFT OUTER JOIN keeps every row from the table on the left side of the join. A RIGHT OUTER JOIN keeps every row from the table on the right side. When a row has no match on the other side, that side’s columns come back as NULL.

OUTER is optional, so LEFT JOIN means the same as LEFT OUTER JOIN, and the same goes for RIGHT. Any right join between two tables can be written as a left join by swapping the tables. That’s why many teams use LEFT JOIN by default and rarely write RIGHT JOIN.

People often ask which join is faster in SQL. Direction decides which rows you get, not how fast the query runs, so choose the join for the answer you need first.

Sample tables used in this article

The examples use two small tables. customers has three people. orders has four orders, and order 104 points to customer 9, which doesn’t exist. That can happen when no foreign key constraint is defined. Chloe has no orders.

Both tables have a customer_id column, so the queries prefix every column with a table alias. That habit is the simplest answer to how to join two tables in SQL with the same column names.

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    name        VARCHAR(50)
);

CREATE TABLE orders (
    order_id    INT PRIMARY KEY,
    customer_id INT,
    amount      DECIMAL(10, 2)
);

INSERT INTO customers (customer_id, name) VALUES
    (1, 'Ana'),
    (2, 'Ben'),
    (3, 'Chloe');

INSERT INTO orders (order_id, customer_id, amount) VALUES
    (101, 1, 50.00),
    (102, 1, 20.00),
    (103, 2, 75.00),
    (104, 9, 30.00);

How a LEFT OUTER JOIN works

SELECT c.name, o.order_id, o.amount
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id;
name order_id amount
Ana 101 50.00
Ana 102 20.00
Ben 103 75.00
Chloe NULL NULL

Every customer appears. Ana shows up twice, once for each of Ana’s two orders. Chloe appears once, with NULL in both order columns, because no order matches Chloe. Order 104 is missing, because a left join keeps every row on the customers side, and that order has no customer to attach to.

Without an ORDER BY clause, the database can return rows in any order, so the order shown here isn’t guaranteed.

How a RIGHT OUTER JOIN works

SELECT c.name, o.order_id, o.amount
FROM customers AS c
RIGHT JOIN orders AS o
    ON o.customer_id = c.customer_id;
name order_id amount
Ana 101 50.00
Ana 102 20.00
Ben 103 75.00
NULL 104 30.00

This time every order appears. Chloe is gone, because no order matches Chloe. Order 104 stays, with a NULL name, because customer 9 doesn’t exist.

Swapping the tables gives the same rows

This left join returns the same four rows as the right join above:

SELECT c.name, o.order_id, o.amount
FROM orders AS o
LEFT JOIN customers AS c
    ON c.customer_id = o.customer_id;

The MySQL manual says RIGHT JOIN works like LEFT JOIN, and it recommends LEFT JOIN when code has to stay portable across databases. Many teams follow that advice.

One catch: with SELECT *, the columns come back in a different order after you swap the tables. Name the columns you need, as the examples here do.

Where the NULLs come from

NULL marks the side of the join that had no match. It isn’t zero and it isn’t an empty string, so arithmetic and comparisons on it need care. A common follow-up is how to get 0 instead of null in SQL for totals like these. COALESCE returns the first value in its list that isn’t NULL:

SELECT c.name,
       COALESCE(SUM(o.amount), 0) AS total_spent
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
GROUP BY c.name;

Ana’s total is 70.00 and Ben’s is 75.00. Chloe shows 0 instead of NULL. Without COALESCE, Chloe’s total would be NULL.

Filters belong in ON when the table is optional

This is one of the most common outer join bugs. A condition on the optional side of the join belongs in the ON clause. Put it in WHERE instead, and the outer join quietly behaves like an inner join.

Here is a query that keeps every customer but only matches orders over 30:

SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
   AND o.amount > 30;

Ana appears with order 101, Ben with order 103, and Chloe with NULL. Ana’s order 102 (20.00) fails the test, so it simply doesn’t match.

Now the same condition in WHERE:

SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
WHERE o.amount > 30;

Only Ana and Ben come back. Chloe’s amount is NULL, and NULL > 30 isn’t true, so WHERE removes Chloe’s row.

As a rule of thumb, a condition on the table you keep goes in WHERE. A condition on the optional table goes in ON.

Finding rows with no match

A left join plus a WHERE test for NULL finds rows that have no partner on the other side. This pattern is often called an anti-join.

SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;

The result is Chloe. Test a column that can’t be NULL on the matched side, such as the primary key order_id. A column that is allowed to be NULL could make a matched row look unmatched.

Joining more than two tables

Each join adds one table to the result so far, so the joins chain from left to right. Inner and outer joins can be mixed, but the order matters. If you’re wondering how to use 3 joins in SQL, the pattern is one JOIN per table, each with its own ON condition.

Suppose a shipments table has one row for order 101, carried by UPS, and one for order 103, carried by FedEx:

SELECT c.name, o.order_id, s.carrier
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
LEFT JOIN shipments AS s
    ON s.order_id = o.order_id;

Chloe stays in the results, because both joins are left joins. If the orders join were an inner join, Chloe’s row would be dropped at that step. When a chain gives unexpected results, check the row count after each join.

Which one should you use?

Knowing when to use left, right, or inner join comes down to one question: which table’s rows must all appear in the result?

  • If the table you’re reading from has to keep every row, use a LEFT JOIN from that table. It usually reads more naturally than a RIGHT JOIN.
  • If you only want rows with a match on both sides, use an inner join.
  • If you need unmatched rows from both sides, you need a full outer join.

If you’re asking what’s the difference between inner and outer join, the short version is that an inner join drops unmatched rows, while outer joins keep some or all of them.

Support across databases

  • MySQL supports LEFT JOIN and RIGHT JOIN, but it has no FULL OUTER JOIN. You can get the same rows by combining a LEFT JOIN with a RIGHT JOIN that keeps only the unmatched orders:
SELECT c.name, o.order_id
FROM customers AS c
LEFT JOIN orders AS o
    ON o.customer_id = c.customer_id
UNION ALL
SELECT c.name, o.order_id
FROM customers AS c
RIGHT JOIN orders AS o
    ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;
  • SQLite added RIGHT and FULL OUTER JOIN in version 3.39.0, released in June 2022. Older versions reject them with an error.
  • PostgreSQL and SQL Server support LEFT, RIGHT and FULL OUTER JOIN.

Summary

LEFT keeps every row of the left table, RIGHT keeps every row of the right table, and the other side fills with NULL when nothing matches. Many people write LEFT JOIN, and swapping the tables is all it takes to switch between the two.

Keep exploring

Guides from other topics

All topics →