RIGHT JOIN in SQL

11/22/2025
All Articles

SQL RIGHT JOIN tutorial with examples and diagrams

RIGHT JOIN in SQL

RIGHT JOIN in SQL – SQL Tutorial for Beginners

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


πŸ”Ή What Is RIGHT JOIN?

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.


πŸ”Έ RIGHT JOIN Syntax

SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;

Here:

  • table1 → Left table

  • table2 → Right table (all rows preserved)


πŸ”Έ Example Tables

πŸ§‘ users table

id name city
1 Amit Delhi
2 Neha Mumbai
3 Rahul Bengaluru

πŸ“¦ orders table

id user_id product
1 1 Mobile
2 3 Laptop
3 5 Tablet

Note: user_id = 5 does not exist in users table.


πŸ”Έ Basic RIGHT JOIN Example

Get all orders and the corresponding users:

SELECT users.name, orders.product
FROM users
RIGHT JOIN orders
ON users.id = orders.user_id;

βœ” Output

name product
Amit Mobile
Rahul Laptop
NULL Tablet

The NULL row means order exists but user not found.


πŸ”Έ RIGHT JOIN with Aliases

SELECT u.name, o.product
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id;

πŸ”Έ RIGHT JOIN with WHERE Clause

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';

πŸ”Έ RIGHT JOIN with Multiple Tables

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;

πŸ”Έ RIGHT JOIN vs LEFT JOIN

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.


πŸ”Έ Real-World Examples

βœ” Orders without users (data mismatch)

SELECT u.name, o.product
FROM users u
RIGHT JOIN orders o
ON u.id = o.user_id;

βœ” Products and categories (all categories shown)

SELECT p.product_name, c.category_name
FROM products p
RIGHT JOIN categories c
ON p.category_id = c.id;

βœ” Students and assigned courses

SELECT s.name, c.course_name
FROM students s
RIGHT JOIN courses c
ON s.course_id = c.id;

πŸ”Ή Best Practices

βœ” 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


⭐ Summary

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.

Article