RIGHT JOIN in SQL
SQL RIGHT JOIN tutorial with examples and diagrams
The RIGHT JOIN is used to return all rows from the right table, and the matching rows from the left table. If there is no match, SQL returns NULL for the left table.
This tutorial explains:
What RIGHT JOIN is
How RIGHT JOIN works
Syntax and examples
RIGHT JOIN vs LEFT JOIN
Real-world use cases
Best practices
A RIGHT JOIN returns:
All rows from the right table
Matching rows from the left table
NULL values where no match is found
It is the opposite of LEFT JOIN.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
Here:
table1 → Left table
table2 → Right table (all rows preserved)
| id | name | city |
|---|---|---|
| 1 | Amit | Delhi |
| 2 | Neha | Mumbai |
| 3 | Rahul | Bengaluru |
| id | user_id | product |
|---|---|---|
| 1 | 1 | Mobile |
| 2 | 3 | Laptop |
| 3 | 5 | Tablet |
Note: user_id = 5 does not exist in users table.
Get all orders and the corresponding users:
SELECT users.name, orders.product
FROM users
RIGHT JOIN orders
ON users.id = orders.user_id;
| name | product |
|---|---|
| Amit | Mobile |
| Rahul | Laptop |
| NULL | Tablet |
The NULL row means order exists but user not found.
SELECT u.name, o.product
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id;
Get all orders made for 'Laptop':
SELECT u.name, o.product
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id
WHERE o.product = 'Laptop';
SELECT u.name, o.product, p.amount
FROM users u
RIGHT JOIN orders o ON u.id = o.user_id
RIGHT JOIN payments p ON o.id = p.order_id;
| Feature | LEFT JOIN | RIGHT JOIN |
|---|---|---|
| Keeps unmatched rows | Left table | Right table |
| NULL values appear | On right table | On left table |
| Most commonly used | β | β (rare) |
RIGHT JOIN is less commonly used because a LEFT JOIN with reversed tables does the same job.
SELECT u.name, o.product
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id;
SELECT p.product_name, c.category_name
FROM products p
RIGHT JOIN categories c
ON p.category_id = c.id;
SELECT s.name, c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;
β Use table aliases for readability
β Prefer LEFT JOIN whenever possible
β Avoid RIGHT JOIN if reversing tables is easier
β Ensure indexes on joined columns
β Use RIGHT JOIN only when right table must be fully included
In this SQL RIGHT JOIN tutorial, you learned:
How RIGHT JOIN works
How it differs from LEFT JOIN
How NULL values represent missing matches
Real-world examples and best practices
RIGHT JOIN is useful when you want all rows from the right table, regardless of matches.