RIGHT JOIN
Learn how RIGHT JOIN mirrors LEFT JOIN, keeping every row from the right table and filling in NULLs for unmatched left rows.
Introduction
You have now seen INNER JOIN (only matches) and LEFT JOIN (everything from the left table). RIGHT JOIN is the third of this family, and it is best understood as simply the mirror image of LEFT JOIN.
This lesson explains what RIGHT JOIN does, works through an example, and explains why many developers rarely reach for it in practice.
- What RIGHT JOIN does and how it mirrors LEFT JOIN.
- A worked example showing every enrollment, even ones tied to a missing student.
- How to rewrite any RIGHT JOIN as an equivalent LEFT JOIN.
- Why LEFT JOIN is generally preferred for readability.
What is a RIGHT JOIN?
A RIGHT JOIN returns ALL rows from the right table (the one named after the JOIN keyword), along with matching rows from the left table. When there is no match on the left side, the columns that would have come from the left table are filled with NULL instead.
RIGHT JOIN and RIGHT OUTER JOIN mean the same thing in MySQL, exactly like LEFT JOIN and LEFT OUTER JOIN.
- RIGHT JOIN keeps every row from the right table, no matter what.
- Matching rows from the left table are attached where they exist.
- Non-matching rows get NULL in every left-table column.
The Sample Tables
To make RIGHT JOIN's behavior obvious, we add one more enrollment row that points to a student_id that does not exist in the students table — imagine a data entry error, or a student who was deleted.
-- students
-- id | name
-- 1 | Alex
-- 2 | Sam
-- 3 | Priya
-- 4 | Owen
INSERT INTO enrollments VALUES (105, 9, 'Data Structures');
-- enrollments now also contains:
-- id | student_id | course_name
-- 105 | 9 | Data StructuresNotice student_id 9 does not exist in the students table at all.
Basic Syntax
SELECT columns
FROM left_table
RIGHT JOIN right_table
ON left_table.column = right_table.column;Here, every row of right_table is guaranteed to appear in the result at least once, regardless of whether left_table has a matching row.
Worked Example
Let's list every enrollment along with the student's name, using RIGHT JOIN so that even the orphaned enrollment (student_id 9) shows up.
SELECT s.name, e.course_name
FROM students AS s
RIGHT JOIN enrollments AS e
ON s.id = e.student_id;+-------+-----------------+
| name | course_name |
+-------+-----------------+
| Alex | MySQL Basics |
| Alex | Web Design |
| Sam | MySQL Basics |
| Priya | JavaScript 101 |
| NULL | Data Structures |
+-------+-----------------+Every row from enrollments made it into the result, including the last one — even though there is no matching student, MySQL fills the name column with NULL rather than dropping the row. Notice, too, that Owen does not appear here, since he has no enrollment row and RIGHT JOIN only guarantees rows from the table on the right.
RIGHT JOIN as a Flipped LEFT JOIN
Here is the key practical insight: any RIGHT JOIN can be rewritten as an equivalent LEFT JOIN, just by swapping the order the two tables are written in. Both queries below produce exactly the same result.
-- Using RIGHT JOIN
SELECT s.name, e.course_name
FROM students AS s
RIGHT JOIN enrollments AS e
ON s.id = e.student_id;
-- The exact same result using LEFT JOIN, tables swapped
SELECT s.name, e.course_name
FROM enrollments AS e
LEFT JOIN students AS s
ON s.id = e.student_id;+-------+-----------------+
| name | course_name |
+-------+-----------------+
| Alex | MySQL Basics |
| Alex | Web Design |
| Sam | MySQL Basics |
| Priya | JavaScript 101 |
| NULL | Data Structures |
+-------+-----------------+All that changed is which table is listed first in FROM — the table you want "all rows guaranteed" from moves to the FROM position, and the join keyword flips from RIGHT to LEFT.
Why Most Developers Prefer LEFT JOIN
Since RIGHT JOIN can always be rewritten as a LEFT JOIN, many developers and teams standardize on using LEFT JOIN everywhere, and simply reorder their FROM clause instead of ever writing RIGHT JOIN. The reasoning is mostly about consistency and readability.
Why LEFT JOIN Wins in Practice
- Reading a long query top to bottom, FROM ... LEFT JOIN ... LEFT JOIN ... reads consistently in one direction.
- Mixing LEFT and RIGHT JOIN in the same multi-table query gets confusing very quickly.
- Most style guides and codebases you will encounter default to LEFT JOIN by convention.
- RIGHT JOIN is still perfectly valid SQL — this is a readability preference, not a technical limitation.
Common Mistakes
- Assuming RIGHT JOIN can do something LEFT JOIN cannot — they are equally powerful, just mirrored.
- Mixing LEFT JOIN and RIGHT JOIN in the same query, making the logic hard to follow.
- Forgetting which table is "right" once a query involves several joins.
- Not realizing NULL can now show up on the left-hand columns, not just the right-hand ones.
Best Practices
- Know how RIGHT JOIN works, but default to LEFT JOIN in your own queries for consistency.
- If you inherit a query using RIGHT JOIN, know you can always mentally flip it to a LEFT JOIN to reason about it.
- Keep join direction consistent throughout a single query when it involves multiple tables.
- Comment complex multi-join queries so future readers do not have to reverse-engineer the intent.
Frequently Asked Questions
Is RIGHT JOIN slower or less efficient than LEFT JOIN?
No. They perform identically; MySQL's query optimizer treats them as equivalent operations, just with tables in a different position.
Is RIGHT OUTER JOIN different from RIGHT JOIN?
No, they are the same. OUTER is an optional keyword you can include or omit.
Should I ever use RIGHT JOIN in real code?
It is valid and occasionally clearer in a specific context, but most teams stick to LEFT JOIN everywhere for consistency, and you will see it far more often in real codebases.
Can I convert every RIGHT JOIN to a LEFT JOIN?
Yes. Swap the order of the two tables in the FROM/JOIN clauses and change RIGHT JOIN to LEFT JOIN; the result is identical.
What happens with three or more tables and RIGHT JOIN?
It still works, but it becomes much harder to read than an equivalent chain of LEFT JOINs, which is another reason LEFT JOIN is generally preferred.
Key Takeaways
- RIGHT JOIN keeps every row from the right table, filling NULL for unmatched left-table columns.
- It is the exact mirror of LEFT JOIN.
- Any RIGHT JOIN query can be rewritten as an equivalent LEFT JOIN by swapping table order.
- Most developers standardize on LEFT JOIN for readability and consistency.
- RIGHT OUTER JOIN and RIGHT JOIN are identical in MySQL.
Summary
RIGHT JOIN behaves exactly like LEFT JOIN, just with the guaranteed table on the other side. Since it can always be rewritten as a LEFT JOIN by reordering the tables, it is more important to recognize RIGHT JOIN when you see it than to write it yourself.
In this lesson, you learned what RIGHT JOIN does, worked through an example with an orphaned enrollment row, and saw exactly how to convert a RIGHT JOIN into an equivalent LEFT JOIN. Next, you will learn CROSS JOIN, which works very differently — it does not use an ON clause at all.
- You understand how RIGHT JOIN mirrors LEFT JOIN.
- You can rewrite any RIGHT JOIN as an equivalent LEFT JOIN.
- You understand why most codebases prefer LEFT JOIN for consistency.
- You are ready to learn CROSS JOIN.