Skip to content
SQLSimplified

intermediate

RIGHT JOIN

Keep every row from the right table and match rows from the left when available.

5 min read

Explanation

A RIGHT JOIN (RIGHT OUTER JOIN) is the mirror image of a LEFT JOIN: it keeps every row from the right (second) table and attaches matching rows from the left table, filling NULL when there's no match.

Because it's just a reversed LEFT JOIN, most developers simply flip the table order and write LEFT JOIN instead. But it's worth understanding so you can read other people's queries.

Prefer LEFT JOIN

If you catch yourself reaching for RIGHT JOIN, ask whether swapping the FROM and JOIN tables and using LEFT JOIN would read better. It usually does.

Syntax

SELECT a.col, b.col
FROM table_a AS a
RIGHT JOIN table_b AS b
  ON a.key = b.key;

Interactive Example

List every department and the employees in it, keeping all departments even if some had no staff. This is the right-table (departments) perspective.

Employees & Departments

Loading database engine...

Employees & Departments

Loading database engine...

Common Mistakes

  • Forgetting which side is preserved. The right table is the one after RIGHT JOIN — not the first one in the query.
  • Filtering the left table in WHERE. Just like with LEFT JOIN, a WHERE condition on the left table can drop the very rows you wanted to keep.
  • Assuming all databases support it. Rewrite as LEFT JOIN if you're unsure about your database engine.

Best Practices

  • Default to LEFT JOIN for readability; reach for RIGHT JOIN only when it genuinely clarifies intent.
  • Keep the preserved table on the left side of a LEFT JOIN so the logic is consistent across your codebase.
  • Use COALESCE on the left-side columns to present NULL matches cleanly.

Practice Question

Rewrite the previous example as a LEFT JOIN (swap the table order) so it returns the same department-and-employee list, and confirm the row counts match.

Summary

RIGHT JOIN keeps all rows of the right table, filling unmatched left columns with NULL. It is functionally identical to a LEFT JOIN with the tables swapped, so most SQL is written with LEFT JOIN instead.

FAQ

Search

Search lessons, functions, examples, and practice problems