Left, Right and Inner Joins

When to use left, right, or inner join?

Pick an inner join for matches only, a left join to keep every row from your main table, and a full join to keep unmatched rows from both, with examples.

On this page
  1. The short answer
  2. The sample tables
  3. When an inner join is the right choice
  4. When a left join is the right choice
  5. When a right join makes sense
  6. When a full join is the right choice
  7. A quick decision guide
  8. Speed is a separate question

The short answer

Ask which rows must appear in the result. An inner join keeps only the rows that match on both sides. A left join keeps every row from the table you’re asking about. A right join keeps every row from the other table, but it’s the same as a left join with the tables swapped, so LEFT is the usual choice. A full join keeps unmatched rows from both sides, which suits reconciling two lists.

If you’re asking what’s the difference between inner and outer join, the inner join is the one that never adds NULL-filled rows. Every outer join can add them.

The sample tables

The examples use two small tables, members and loans:

CREATE TABLE members (
    member_id INT PRIMARY KEY,
    name      VARCHAR(50)
);

CREATE TABLE loans (
    loan_id    INT PRIMARY KEY,
    member_id  INT,
    book_title VARCHAR(100)
);

INSERT INTO members (member_id, name) VALUES
    (1, 'Ana'),
    (2, 'Ben'),
    (3, 'Chloe');

INSERT INTO loans (loan_id, member_id, book_title) VALUES
    (1, 1, 'Dune'),
    (2, 1, 'Emma'),
    (3, 2, 'Ulysses'),
    (4, 9, 'Middlemarch');

Chloe has no loans. The last loan belongs to member 9, who isn’t in the members table.

When an inner join is the right choice

Use an inner join when a row without a match isn’t useful. Suppose you want a list of members who have borrowed a book:

SELECT m.name AS member, l.book_title AS book
FROM members AS m
INNER JOIN loans AS l
    ON l.member_id = m.member_id;
member book
Ana Dune
Ana Emma
Ben Ulysses

Chloe is left out because there is no loan to match. The loan for member 9 is left out because there is no member to match. For this question, both of those rows are noise, so the inner join is the right tool.

When a left join is the right choice

Use a left join when the table you’re asking about must appear in full. Suppose you want every member, with a book if they have one:

SELECT m.name AS member, l.book_title AS book
FROM members AS m
LEFT JOIN loans AS l
    ON l.member_id = m.member_id;
member book
Ana Dune
Ana Emma
Ben Ulysses
Chloe NULL

Chloe now appears, with NULL for the book. The loan for member 9 still doesn’t appear, because the members table is the one being kept.

When a right join makes sense

A right join keeps every row from the table on the right. Here that means every loan, including the one for member 9. Many people write the same result as a left join with the tables swapped:

SELECT m.name AS member, l.book_title AS book
FROM loans AS l
LEFT JOIN members AS m
    ON m.member_id = l.member_id;

The rows are the same as a RIGHT JOIN from members to loans would give: Ana with Dune, Ana with Emma, Ben with Ulysses, and NULL with Middlemarch. The MySQL manual recommends LEFT JOIN over RIGHT JOIN for code that has to stay portable, so the swapped version is usually the better habit.

When a full join is the right choice

Use a full outer join when you need the unmatched rows from both sides. A common case is reconciling two lists, such as members in one system and loans in another. You want to see members without loans and loans without members in one result.

SELECT m.name AS member, l.book_title AS book
FROM members AS m
FULL OUTER JOIN loans AS l
    ON l.member_id = m.member_id;
member book
Ana Dune
Ana Emma
Ben Ulysses
Chloe NULL
NULL Middlemarch

Chloe appears because there is no loan for Chloe, and the Middlemarch loan appears because its member isn’t in the members table.

MySQL doesn’t support FULL OUTER JOIN, so there you’d use the UNION workaround. SQLite supports it from version 3.39.0.

A quick decision guide

  • Only matches matter: use an inner join.
  • Every row from the table you’re asking about matters: start with a left join from that table.
  • You need the unmatched rows from both sides: use a full outer join, or emulate one with a LEFT JOIN, a RIGHT JOIN and UNION if your database lacks it.
  • You find yourself writing a right join: swap the tables and write a left join instead.

A CROSS JOIN doesn’t belong in this decision. It pairs every row with every row and is for building combinations, not for matching related rows.

Speed is a separate question

Once the join gives the right rows, you may wonder which join is faster in SQL. That depends on indexes, table sizes and the query plan, not on the join’s name. Choose by correctness first, then measure.

The complete guide to left, right and inner joins

Left Outer Join vs Right Outer Join in SQL
Read the guide

Keep exploring

Guides from other topics

All topics →