SQL RIGHT JOIN
SQL

SQL RIGHT JOIN — How to Include All Rows from the Right Table

CHAPTER 23 of 30 SQL Tutorial — Free Course on Neotech Navigators
The SQL RIGHT JOIN returns all rows from the right table and the matching rows from the left table. If there is no match, the left columns are filled with NULL. In this chapter, you will learn the RIGHT JOIN syntax, see practical examples, and understand when to use RIGHT JOIN vs LEFT JOIN.

SQL RIGHT JOIN Syntax

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

RIGHT JOIN is also called RIGHT OUTER JOIN — both are identical. The keyword OUTER is optional.

Sample Tables

We will use the same Employees and Departments tables from the Joins Introduction chapter:

TABLE: Employees
+----+-----------+--------+--------+
| ID | Name      | DeptID | Salary |
+----+-----------+--------+--------+
| 1  | Priya K.  | 101    | 55000  |
| 2  | John D.   | 102    | 62000  |
| 3  | Arun M.   | 101    | 78000  |
| 4  | Emma W.   | 103    | 51000  |
| 5  | Ravi S.   | NULL   | 72000  |
+----+-----------+--------+--------+
TABLE: Departments
+-----+----------------+-----------+
| ID  | DeptName       | Location  |
+-----+----------------+-----------+
| 101 | IT             | Delhi     |
| 102 | Marketing      | London    |
| 103 | Sales          | Mumbai    |
| 104 | HR             | New York  |
+-----+----------------+-----------+

Notice that Ravi S. has a NULL DeptID (no department), and the HR department (ID 104) has no employees assigned to it.

SQL RIGHT JOIN Example

This query returns all departments with their employees. Departments without employees still appear:

SQL
SELECT e.Name, d.DeptName, d.Location
FROM Employees AS e
RIGHT JOIN Departments AS d
  ON e.DeptID = d.ID;
✓ OUTPUT
+-----------+-----------+-----------+
| Name      | DeptName  | Location  |
+-----------+-----------+-----------+
| Priya K.  | IT        | Delhi     |
| Arun M.   | IT        | Delhi     |
| John D.   | Marketing | London    |
| Emma W.   | Sales     | Mumbai    |
| NULL      | HR        | New York  |
+-----------+-----------+-----------+

The HR department appears with NULL for Name because no employee is assigned to department 104. Ravi S. is excluded because RIGHT JOIN only guarantees all rows from the right table (Departments).

RIGHT JOIN with WHERE Filter

You can add a WHERE clause to filter the results further. This query finds departments that have no employees:

SQL — Find Empty Departments
SELECT d.DeptName, d.Location
FROM Employees AS e
RIGHT JOIN Departments AS d
  ON e.DeptID = d.ID
WHERE e.ID IS NULL;
✓ OUTPUT
+-----------+-----------+
| DeptName  | Location  |
+-----------+-----------+
| HR        | New York  |
+-----------+-----------+

The trick is checking WHERE e.ID IS NULL — this returns only the rows from the right table that had no match in the left table.

RIGHT JOIN vs LEFT JOIN

RIGHT JOIN and LEFT JOIN are mirror images of each other. Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping the table order:

RIGHT JOIN Equivalent LEFT JOIN
FROM Employees RIGHT JOIN Departments ON ... FROM Departments LEFT JOIN Employees ON ...

Both queries produce the same result. Here is the LEFT JOIN equivalent:

SQL — Same Result Using LEFT JOIN
SELECT e.Name, d.DeptName, d.Location
FROM Departments AS d
LEFT JOIN Employees AS e
  ON e.DeptID = d.ID;

💡 When Should You Use RIGHT JOIN?

In practice, most developers prefer LEFT JOIN over RIGHT JOIN because it reads more naturally — you start with your main table and add related data. RIGHT JOIN is logically equivalent but less common. Use whichever makes your query easier to read.

RIGHT JOIN vs RIGHT OUTER JOIN

RIGHT JOIN and RIGHT OUTER JOIN are exactly the same. The keyword OUTER is optional and does not change the behavior:

SQL — Both Are Identical
SELECT e.Name, d.DeptName
FROM Employees AS e
RIGHT JOIN Departments AS d ON e.DeptID = d.ID;

SELECT e.Name, d.DeptName
FROM Employees AS e
RIGHT OUTER JOIN Departments AS d ON e.DeptID = d.ID;

For the complete JOIN syntax reference, see the official MySQL JOIN documentation. In the next chapter, you will learn SQL FULL JOIN to return all rows from both tables.

If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.

📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.

🧪 Try It Yourself

Write a RIGHT JOIN query that shows all departments with the total salary of their employees. Departments with no employees should show 0.

ANSWER
SELECT d.DeptName,
       COALESCE(SUM(e.Salary), 0) AS TotalSalary
FROM Employees AS e
RIGHT JOIN Departments AS d
  ON e.DeptID = d.ID
GROUP BY d.DeptName;

Expected Output:

+-----------+-------------+
| DeptName  | TotalSalary |
+-----------+-------------+
| HR        | 0           |
| IT        | 133000      |
| Marketing | 62000       |
| Sales     | 51000       |
+-----------+-------------+

📝 What You Learned in This Chapter

  • RIGHT JOIN returns all rows from the right table and matching rows from the left
  • Unmatched left-table columns are filled with NULL
  • RIGHT JOIN and RIGHT OUTER JOIN are identical
  • Any RIGHT JOIN can be rewritten as a LEFT JOIN by swapping tables
  • Use WHERE left.column IS NULL to find unmatched right-table rows
  • Most developers prefer LEFT JOIN over RIGHT JOIN for readability

Frequently Asked Questions

What is a SQL RIGHT JOIN?
A RIGHT JOIN returns all rows from the right table and the matching rows from the left table. If a row in the right table has no match in the left table, the left columns are filled with NULL. It guarantees that every row from the right table appears in the result.
What is the difference between RIGHT JOIN and LEFT JOIN?
LEFT JOIN keeps all rows from the left table. RIGHT JOIN keeps all rows from the right table. They are mirror images — A RIGHT JOIN B is the same as B LEFT JOIN A. Most developers prefer LEFT JOIN because it reads more naturally.
Is RIGHT OUTER JOIN the same as RIGHT JOIN?
Yes. RIGHT JOIN and RIGHT OUTER JOIN are exactly the same. The keyword OUTER is optional and does not change the query behavior. Most developers omit OUTER for shorter syntax.
When should I use RIGHT JOIN instead of LEFT JOIN?
Use RIGHT JOIN when your main table is on the right side and you want to keep all its rows. In practice, LEFT JOIN is preferred because it reads top-to-bottom naturally. You can always rewrite a RIGHT JOIN as a LEFT JOIN by swapping the table order.
Does MySQL support RIGHT JOIN?
Yes. MySQL, PostgreSQL, SQL Server, and Oracle all support RIGHT JOIN. It works the same way across all major SQL databases.

📖 Chapter 23 of 30 — SQL Tutorial on Neotech Navigators

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com