MySQL — Right Join
Basic RIGHT JOIN
SELECT * FROM users
RIGHT JOIN orders ON users.id = orders.user_id;
RIGHT JOIN Examples
-- All orders with user info
SELECT
u.name,
o.total,
o.created_at
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id;
-- Orders without valid users
SELECT o.*
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id
WHERE u.id IS NULL;
-- All products, even not ordered
SELECT
p.name,
COALESCE(SUM(oi.quantity), 0) as total_sold
FROM products p
RIGHT JOIN order_items oi ON p.id = oi.product_id
GROUP BY p.id;
RIGHT JOIN vs LEFT JOIN
-- RIGHT JOIN
SELECT * FROM users
RIGHT JOIN orders ON users.id = orders.user_id;
-- Equivalent LEFT JOIN (reversed tables)
SELECT * FROM orders
LEFT JOIN users ON orders.user_id = users.id;
Mini Practice
Write MySQL code that:
- Uses RIGHT JOIN
- Finds unmatched records
- Compares with LEFT JOIN
- Uses RIGHT JOIN with aggregates
Up Next
Next: Learn about Cross Join.
Related Topics
Frequently Asked Questions about Right Join
What is Right Join in MySQL?
Right Join is a fundamental concept in MySQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Right Join?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Right Join.
Why is Right Join important in MySQL?
Right Join is essential for MySQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.