RIGHT JOIN
ELI5 — The Vibe Check
RIGHT JOIN is LEFT JOIN's mirror image — it returns all rows from the right table, and matching rows from the left. Nobody uses RIGHT JOIN in practice because you can always rewrite it as a LEFT JOIN by swapping the table order. But it exists.
Real Talk
RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right (second) table regardless of whether a matching row exists in the left table. It is functionally equivalent to LEFT JOIN with the tables swapped. Most developers prefer LEFT JOIN for consistency.
Show Me The Code
-- Equivalent to swapping tables in a LEFT JOIN
SELECT u.name, o.total
FROM orders o
RIGHT JOIN users u ON o.user_id = u.id;
When You'll Hear This
"RIGHT JOIN works but most developers just flip the tables and use LEFT JOIN instead."
Related Terms
FULL JOIN
FULL JOIN returns everything from both tables regardless of whether there is a match. Rows with no match on either side get NULLs.
INNER JOIN
INNER JOIN only returns rows where there is a match in BOTH tables. If a user has no orders, they do not appear in the result.
JOIN
JOIN combines rows from two tables based on a related column.
LEFT JOIN
LEFT JOIN returns all rows from the left table, and matching rows from the right table.