RIGHT and FULL OUTER JOIN, and Why You Rarely Need Them

Most beginners learn INNER JOIN and LEFT JOIN first, then wonder about the other two in the list. This post explains RIGHT JOIN and FULL OUTER JOIN with two tiny tables, and why you can go a long way without them.

Writing your first SQL queries illustration

Two tiny tables

students              clubs
id | name             id | student_id | club
1  | Ana              10 | 1          | Chess
2  | Ben              11 | 1          | Drama
3  | Chi              12 | 4          | Robotics

Ana is in two clubs. Ben and Chi are in none. The Robotics row points to student 4, who does not exist in students (perhaps they left).

A quick reminder: LEFT JOIN

SELECT s.name, c.club
FROM students s
LEFT JOIN clubs c ON c.student_id = s.id;

Result: every student, with their clubs if any. Ben and Chi appear with an empty (NULL) club. The orphaned Robotics row does not appear, because it matches no student.

RIGHT JOIN

A RIGHT JOIN keeps every row from the table on the right side instead:

SELECT s.name, c.club
FROM students s
RIGHT JOIN clubs c ON c.student_id = s.id;

Result: every club row. Chess and Drama show Ana. Robotics shows an empty name, because student 4 is missing. Ben and Chi do not appear.

Here is the secret: a RIGHT JOIN is the same as a LEFT JOIN with the tables swapped.

SELECT s.name, c.club
FROM clubs c
LEFT JOIN students s ON c.student_id = s.id;

That is why many people never write RIGHT JOIN. Reading left to right, "start from this table and add matches from that one" is easier to follow.

FULL OUTER JOIN

A FULL OUTER JOIN keeps unmatched rows from both sides:

SELECT s.name, c.club
FROM students s
FULL OUTER JOIN clubs c ON c.student_id = s.id;

Result: Ana with Chess, Ana with Drama, Ben with no club, Chi with no club, and Robotics with no student.

Summary table

JoinKeeps unmatched rows fromTypical use
INNER JOINNeither sideOnly pairs that match
LEFT JOINThe left table"All students, with clubs if any"
RIGHT JOINThe right tableSame as LEFT with sides swapped
FULL OUTER JOINBoth tablesComparing two lists to find gaps on either side

Engine notes

PostgreSQL and SQL Server support all four. MySQL does not support FULL OUTER JOIN directly; people combine a LEFT JOIN and a RIGHT JOIN with UNION. SQLite added RIGHT and FULL OUTER JOIN in version 3.39.0, released in 2022, so older copies will give a syntax error.

When FULL OUTER JOIN is genuinely handy

It shines when checking two lists against each other, such as "which rows exist in this table but not that one, and the other way round". Our example found an orphaned club row that way. In everyday app queries, LEFT JOIN covers nearly everything. If LEFT JOIN still feels shaky, start with INNER and LEFT JOIN, one small step at a time.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

Get new pages by email

Related pages on this site