RIGHT JOIN
RIGHT JOIN is the mirror of LEFT JOIN — it returns ALL rows from the right table and matching rows from the left. In practice, RIGHT JOIN is rarely used because you can always rewrite it as a LEFT JOIN by swapping table order.
RIGHT JOIN Key Points
- ALL rows from right table — always
- Matching rows from left table — when available
- NULL for left-table columns when no match
- Functionally identical to LEFT JOIN with tables swapped
- Most developers rewrite RIGHT JOINs as LEFT JOINs for consistency
RIGHT JOIN Examples
-- All orders and their users (even if user somehow deleted)
SELECT
u.name,
o.id AS order_id,
o.status
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id;
-- All orders appear; user info may be NULL if user deleted
-- Equivalent LEFT JOIN (preferred style):
SELECT
u.name,
o.id AS order_id,
o.status
FROM orders o
LEFT JOIN users u ON o.user_id = u.id;
-- Same result — just swap table order
-- RIGHT JOIN to ensure all products appear
SELECT
o.id AS order_id,
p.name AS product
FROM orders o
RIGHT JOIN products p ON o.id = p.id;
-- All products appear even if never orderedQuick Quiz
Tip
Tip
Practice RIGHT JOIN in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Inner query runs first, results used by outer query. CTEs are often more readable.
Common Mistake
Warning
A common mistake with RIGHT JOIN is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of RIGHT JOIN from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- RIGHT JOIN is the mirror of LEFT JOIN — it returns ALL rows from the right table and matching rows from the left.
- ALL rows from right table — always
- Matching rows from left table — when available
- NULL for left-table columns when no match