Left, Right and Inner Joins

How to use 3 JOINs in SQL?

Chain one JOIN per extra table, each with its own ON condition. Learn how inner and left joins behave across three tables, with a worked example and output.

On this page
  1. The short answer
  2. The sample tables
  3. Chain the joins one at a time
  4. Make the outer join the first one
  5. Common mistakes with three or more joins
  6. Check the row count at each step

The short answer

To use three joins in SQL, start with the first table in FROM, then add one JOIN for each additional table. Each JOIN needs its own ON condition that links the new table to a table already in the query.

SELECT a.id, b.id, c.id
FROM table_a AS a
JOIN table_b AS b
    ON b.a_id = a.id
JOIN table_c AS c
    ON c.b_id = b.id;

Each join can be inner or outer. Choose each one based on whether the rows on that side must survive.

The sample tables

The examples use three small tables: authors, books and reviews.

CREATE TABLE authors (
    author_id INT PRIMARY KEY,
    name      VARCHAR(50)
);

CREATE TABLE books (
    book_id   INT PRIMARY KEY,
    author_id INT,
    title     VARCHAR(100)
);

CREATE TABLE reviews (
    review_id INT PRIMARY KEY,
    book_id   INT,
    rating    INT
);

INSERT INTO authors (author_id, name) VALUES
    (1, 'Ana Lopez'),
    (2, 'Ben Carter'),
    (3, 'Chloe Kim');

INSERT INTO books (book_id, author_id, title) VALUES
    (10, 1, 'SQL Basics'),
    (11, 1, 'Data Modeling'),
    (12, 2, 'Query Tuning');

INSERT INTO reviews (review_id, book_id, rating) VALUES
    (100, 10, 5),
    (101, 10, 4),
    (102, 12, 3);

Chloe Kim has no books, and Data Modeling has no reviews. SQL Basics has two reviews.

Chain the joins one at a time

If you’re still learning how to write an inner join query in SQL, the syntax below is the same one you’d use for a single join. This query joins authors to books with an inner join, then adds reviews with a left join:

SELECT a.name AS author, b.title, r.rating
FROM authors AS a
INNER JOIN books AS b
    ON b.author_id = a.author_id
LEFT JOIN reviews AS r
    ON r.book_id = b.book_id;
author title rating
Ana Lopez SQL Basics 5
Ana Lopez SQL Basics 4
Ana Lopez Data Modeling NULL
Ben Carter Query Tuning 3

SQL Basics appears twice because it has two reviews. Data Modeling stays, with NULL for the rating, because the second join is a left join. Chloe Kim is missing because the first join is an inner join, and there are no books to match.

The first join decides which authors survive. The second join only adds reviews to the books that made it through.

Make the outer join the first one

If you want every author in the result, change the first join to a LEFT JOIN:

SELECT a.name AS author, b.title, r.rating
FROM authors AS a
LEFT JOIN books AS b
    ON b.author_id = a.author_id
LEFT JOIN reviews AS r
    ON r.book_id = b.book_id;

The four rows from before stay the same, and one more row appears for Chloe Kim, with NULL in both the title and the rating. Swapping one join type changes the answer, so check each one deliberately.

Common mistakes with three or more joins

  • A join can only use tables that already appear. The condition ON r.book_id = b.book_id works only because books comes before reviews in the query.
  • Shared column names need a table prefix. Columns such as author_id and book_id exist in more than one table, so write b.author_id and a.author_id rather than the bare name. This is the same issue behind how to join two tables in SQL with the same column names.
  • A missing ON condition can multiply rows. Some databases reject a JOIN without ON. Others, such as MySQL, accept it and quietly create a cross join, which pairs every row with every row.
  • Matches multiply rows. A book with several reviews repeats in the result, once for each review. That’s expected, but it can surprise you when you count rows.

Check the row count at each step

Run the query after each join and count the rows. If the count jumps more than you expected, a join is matching more rows than you meant. If it drops, an inner join is probably removing rows you wanted to keep. The fix is usually a different join type, a more specific ON condition or a filter in WHERE.

Each join deserves the same question you’d ask about a two-table query: must every row on this side appear? When to use left, right, or inner join answers that question for one join at a time, and the answer can differ from one step to the next.

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 →