🎓 A1Learner
🔍

Home / Blog / Databases / SQL

SQL Join Types Explained: Inner, Left, Right, and Full

Databases · SQL · 6 min read

A plain-words guide to SQL joins, worked row by row through one small example so you can predict exactly what each join returns.

What a Join Pairs Together

A library keeps member names in one table and borrowed books in another. A table is a set of rows, where each row is one record and each column holds one kind of fact about that record. Keeping the two lists apart avoids repeating every member name beside every loan, but it creates a question: how do you show a name next to the book that person borrowed?

A join answers that question in a single query. It pairs rows from two tables where a shared value matches. That shared value is usually a key, which is a column that identifies a row, such as a member number. In the example used here, the members table holds Ana with id 1, Ben with id 2, and Cleo with id 3. The loans table holds four loans: Ana borrowed A Garden of Days, Ben borrowed The Winter Market and Harbour Lights, and one loan lists member 9, a member who no longer exists. The join type you choose decides what happens to rows that find no partner, and that is the whole difference between the four joins below.

The words left and right simply describe the order in the query. The table named before the word JOIN is the left table. The table named after it is the right table. Every example below joins members on the left to loans on the right, on the condition that members.id equals loans.member_id.

SELECT members.name, loans.book
FROM members
JOIN loans ON members.id = loans.member_id;
The basic join pattern: two tables, one matching condition.

Inner Join: Only the Matches

An inner join returns only rows that match on both sides. A row with no partner is left out, whichever side it sits on. The word INNER is optional, so a plain JOIN means the same thing, but writing INNER makes the intent clear to the next reader.

Walk through the library example row by row. Ana matches her one loan, so the result holds Ana with A Garden of Days. Ben matches two loans, so he appears twice, once with The Winter Market and once with Harbour Lights. Cleo has no loan, so she drops out. The loan for member 9 has no member, so it drops out too. The result has three rows, and every row is a genuine pair.

SELECT members.name, loans.book
FROM members
INNER JOIN loans ON members.id = loans.member_id;
Inner join: three rows, all matched pairs, nothing else.

Left Join: Keep Every Row on the Left

A left join keeps every row from the left table, whether or not it finds a partner. When a left row has no match, the columns from the right table are filled with NULL. NULL is SQL's marker for a missing or unknown value, not a zero and not an empty piece of text.

In the library example, a left join returns four rows. Ana appears once and Ben appears twice, exactly as before. Cleo now appears as well, with NULL where the book title would go, because she has borrowed nothing. The loan for member 9 still drops out, since a left join only protects the left side. This makes left join the natural choice for a report that must list everyone, including people with nothing to show yet, such as all members and any books they have borrowed.

SELECT members.name, loans.book
FROM members
LEFT JOIN loans ON members.id = loans.member_id;
Left join: four rows, with Cleo kept and her book shown as NULL.

Right Join and Full Join: The Other Side, and Both Sides

A right join is the mirror image of a left join. It keeps every row from the right table and fills the left columns with NULL where no partner exists. In the library example, a right join returns four rows: the three matched pairs, plus the loan for member 9 with NULL where the member name would go. Cleo drops out this time, because the right join only protects the right side.

A full join keeps every row from both tables. Matched rows appear together, and unmatched rows from either side appear with NULL on the missing side. The library example gives five rows: the three matched pairs, Cleo with a NULL book, and the member 9 loan with a NULL name. Full join is less common in everyday queries, but it is the right tool when you need to reconcile two lists, for instance comparing members against loans to spot both inactive members and orphaned loans in one pass.

In practice, most writers rarely use right join. Swapping the two tables and writing a left join gives the same result and usually reads more naturally, because readers tend to scan from the main list outward. Right join remains worth recognising, because you will meet it in other people's queries.

SELECT members.name, loans.book
FROM members
FULL OUTER JOIN loans ON members.id = loans.member_id;
Full join: five rows, with unmatched rows kept on both sides.

Choosing the Right Join

Start from the question, not from habit. If a row without a partner has nothing useful to say, use an inner join. A list of books currently on loan needs only genuine pairs, so unmatched members and orphaned loans would only clutter it. If the report must be complete for one list, use a left join with that list on the left. A list of all members, with borrowed books where they exist, is a left join.

Use a full join when both lists need checking against each other. A stocktake that must surface members who borrowed nothing and loans that point nowhere needs both kinds of gap in one result. If you catch yourself writing a right join, try swapping the tables first. A left join that reads in the order of your main list is easier to check later, and checking matters more than cleverness.

SELECT members.name
FROM members
LEFT JOIN loans ON members.id = loans.member_id
WHERE loans.loan_id IS NULL;
Finding the missing rows: this returns Cleo, the only member with no loan.

Two Mistakes That Change Your Answer

The first mistake is filtering the right table in the wrong place. Suppose you write a left join and then add a condition on a loans column in the WHERE clause, such as asking for a particular book title. A row like Cleo's has NULL in every loans column, and NULL fails that comparison, so her row is discarded. The left join quietly behaves like an inner join, and the very rows you wanted to keep vanish. Conditions that describe how rows match belong in the ON clause. Conditions that describe which final rows you want belong in WHERE, but only once you accept that they can remove unmatched rows.

The second mistake is assuming one row in means one row out. Ben appears twice in the inner join result because he has two loans, not because anything went wrong. A join multiplies rows when one side has several matches. If a count looks too high, check whether the right table holds duplicates for the same key before blaming the join type. Counting the partners for a single key, on paper if needed, settles most join surprises quickly.

Questions readers ask

What is the difference between an inner join and a left join?

An inner join returns only rows that match on both sides. A left join returns every row from the left table, with NULL in the right columns where no match exists.

Is a right join just a left join written backwards?

Yes, in effect. Swapping the two tables and using a left join gives the same result as the original right join. Most writers prefer the left join version because it reads in the order of the main list.

How do I find rows that have no match in the other table?

Use a left join, then keep only rows where a right-table key is NULL. In the library example, that pattern returns Cleo, the member with no loan.

Why does a full join show NULL values on both sides?

A full join keeps unmatched rows from both tables. A row kept from one side has no partner on the other, so the other side's columns are filled with NULL.