RIGHT JOIN in SQL

11/22/2025

SQL RIGHT JOIN tutorial with examples and diagrams

Go Back

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


SQL RIGHT JOIN tutorial with examples and diagrams

🔹 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

idnamecity
1AmitDelhi
2NehaMumbai
3RahulBengaluru

📦 orders table

iduser_idproduct
11Mobile
23Laptop
35Tablet

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

nameproduct
AmitMobile
RahulLaptop
NULLTablet

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

FeatureLEFT JOINRIGHT JOIN
Keeps unmatched rowsLeft tableRight table
NULL values appearOn right tableOn 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.