codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

25 / 67 topics
22Joins Basics23INNER JOIN24LEFT JOIN25RIGHT JOIN26FULL OUTER JOIN27Self Join
Tutorials/SQL & Databases/RIGHT JOIN
🗄️SQL & Databases

RIGHT JOIN

Updated 2026-05-15
10 min read

RIGHT JOIN

Introduction

In SQL, a RIGHT JOIN is one of the fundamental types of joins used to combine rows from two or more tables based on a related column between them. Unlike other join types like INNER JOIN, LEFT JOIN, and FULL OUTER JOIN, a RIGHT JOIN returns all records from the right table and the matched records from the left table. If there is no match, the result is NULL on the side of the left table.

This type of join is particularly useful when you want to ensure that all records from one specific table are included in your results, regardless of whether they have corresponding entries in another table.

Concept

The basic syntax for a RIGHT JOIN is as follows:

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

Here's a breakdown of the components:

  • table1: The left table.
  • table2: The right table.
  • common_column: The column that is common to both tables and used for matching records.

The RIGHT JOIN will return all rows from table2, along with the matched rows from table1. If there are no matches, the columns from table1 will contain NULL.

Examples

Let's illustrate the concept of a RIGHT JOIN with some practical examples. We'll use two tables: employees and departments.

Example 1: Basic RIGHT JOIN

Suppose we have the following tables:

Table: employees

employee_idemployee_namedepartment_id
1Alice1
2Bob2
3CharlieNULL

Table: departments

department_iddepartment_name
1HR
2Finance
4Marketing

Now, let's perform a RIGHT JOIN to get all departments and their corresponding employees:

SELECT employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;

Output:

employee_namedepartment_name
AliceHR
BobFinance
NULLMarketing

In this example, all records from the departments table are returned. The Marketing department has no corresponding employee in the employees table, so the employee_name is NULL.

Example 2: Including All Columns

You can also include all columns from both tables in your result set:

SELECT *
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;

Output:

employee_idemployee_namedepartment_iddepartment_iddepartment_name
1Alice11HR
2Bob22Finance
NULLNULLNULL4Marketing

This example shows all columns from both tables, with NULL values where there are no matches.

Example 3: Filtering Results

You can also apply filters to the results of a RIGHT JOIN. For instance, let's filter to show only departments that have employees:

SELECT employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id
WHERE employees.employee_name IS NOT NULL;

Output:

employee_namedepartment_name
AliceHR
BobFinance

This query excludes the Marketing department because it has no corresponding employee.

What's Next?

After mastering the RIGHT JOIN, you might want to explore other types of joins, such as the FULL OUTER JOIN. This type of join returns all records when there is a match in either left or right table. You can find more information on this topic in our next tutorial.

If you have any questions or need further clarification on RIGHT JOIN and its applications, feel free to ask!


PreviousLEFT JOINNext FULL OUTER JOIN

Recommended Gear

LEFT JOINFULL OUTER JOIN