Skip to content

SQL RIGHT JOIN

RIGHT JOIN keeps every row of the right (second) table. Left-hand columns become NULL where there is no match.

pick a join type
customers
idname
1Ada
2Grace
3Linus
4Hedy
orders
idcustomer_id
1011
1022
1031
1089
customersorders
SELECT c.name, o.id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id;

Every order, matched or not. Order 108 appears with a NULL name. Most people rewrite this as a LEFT JOIN with the tables swapped.

Result · 4 rows
nameorder idtotal
Ada101131
Grace10234.5
Ada1039.5
NULL10842

Run this on the full sample database

RIGHT JOIN example, row by row

Every order is kept. Order 108 belongs to customer 9, who does not exist, so its name is NULL. Customers without orders are dropped.

SELECT c.name, o.id, o.total
FROM customers c
RIGHT JOIN orders o ON o.customer_id = c.id;

▶ Run it on the full sample database

What to know about RIGHT JOIN

  • RIGHT JOIN and RIGHT OUTER JOIN are identical.
  • Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the table order; most style guides prefer LEFT JOIN for readability.
  • SQLite supports RIGHT JOIN from version 3.39 (2022); older SQLite builds reject it.

Other join types

Syntax for everything else, from GROUP BY to window functions, is on the SQL cheat sheet.

RIGHT JOIN questions

What is a RIGHT JOIN in SQL?

A RIGHT JOIN returns all rows from the right (second) table and the matching rows from the left table, with NULL in the left-hand columns where no match exists.

What is the difference between a LEFT JOIN and a RIGHT JOIN?

Only which side is preserved. a LEFT JOIN b keeps all of a; a RIGHT JOIN b keeps all of b. Swapping the tables turns one into the other.

When should I use a RIGHT JOIN?

Rarely. It is useful when you add a table to the end of a long chain of joins and want to keep all of that table’s rows, but rewriting the query with LEFT JOIN is usually clearer.

Does MySQL support RIGHT JOIN?

Yes. MySQL, PostgreSQL, SQL Server, Oracle and SQLite 3.39+ all support RIGHT JOIN. MySQL lacks FULL OUTER JOIN, not RIGHT JOIN.