VillageSQL is a drop-in replacement for MySQL with extensions.
All examples on this page work on VillageSQL. Install Now →
RIGHT JOIN keeps every row of the table on the right, matched or not. It is
the mirror image of LEFT JOIN and it does nothing a
LEFT JOIN cannot do, so this lesson is short. It exists because you will read
it in other people’s SQL.
Open the client with mysql -u root -p sakila before you start.
How RIGHT JOIN works
COLUMN_LIST with the columns you want, LEFT_TABLE with the table you
start from, RIGHT_TABLE with the table every row of which you want to keep,
and each COLUMN_NAME with the column on that side that links them. A
right row that matches nothing comes back with every column of the left table
set to NULL. RIGHT OUTER JOIN is the same clause spelled out.
Examples
1) Keep every row of the second table
film is on the left and language on the right, so this keeps all 6
languages.
LEFT JOIN lesson got from the same 2 tables in
the other order.
2) List the rows that matched nothing
The same pattern as a left join, with the NULL test on the left table now.Every RIGHT JOIN can be written as a LEFT JOIN
a RIGHT JOIN b returns the same rows as b LEFT JOIN a. Swap the 2 table
names, swap the keyword, and the answer does not move. With SELECT * the
columns come back in a different order, because * expands in the order the
tables appear in FROM, so name your columns if the order matters.
Most SQL you read is written with LEFT JOIN for that reason: a query is
easier to follow when every join in it leans the same way.
Everything else about it, including the WHERE trap, works exactly as the
LEFT JOIN lesson describes, with left and right
exchanged.
Summary
- Use
RIGHT JOINto keep every row of the table named second. - Expect NULL in every left-hand column of a row that matched nothing.
- Read
a RIGHT JOIN basb LEFT JOIN a, and name your columns rather than using*. - Prefer
LEFT JOINin SQL you write, so the joins in a query all lean the same way.
See also
- LEFT JOIN — the same behavior, and the traps that come with it
- INNER JOIN — the join that keeps only matches
- How joins work — what separates the kinds of join
- MySQL JOIN performance and common mistakes — why a reviewer asks you to rewrite it

