Skip to main content
Log inGet a demo
Back to SQL Dictionary
Joins

SQL RIGHT JOIN

What is SQL RIGHT JOIN?

SQL RIGHT JOIN, also known as a RIGHT OUTER JOIN, is a type of SQL JOIN operation that retrieves all records from the right table (table2) and the matching records from the left table (table1). If there are no matching records in the left table, NULL values are included for those columns.

When you would use it

You would use a SQL RIGHT JOIN when you want to retrieve all the records from one table (the right table) and only the matching records from a related table (the left table). Common use cases for RIGHT JOIN include:

  1. Displaying all items: When you want to display all items from the right table, even if there are no corresponding items in the left table.

  2. Handling missing data: When you need to handle situations where data might be missing or incomplete in one of the tables.

  3. Optional relationships: In scenarios where relationships between tables are optional, and you still want to display data from the secondary table.

  4. Combining data: When you want to combine data from multiple tables while preserving all records from one of the tables.

Syntax

The syntax for a SQL RIGHT JOIN operation is as follows:

SELECT column_list
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
  • column_list: A list of columns to retrieve from the joined tables.
  • table1 and table2: The names of the tables to be joined.
  • column: The common column or key that relates the tables.

Parameter values

  • column_list: A list of column names you want to select from the joined tables.
  • table1 and table2: The names of the tables to be joined.
  • column: The related column or key that connects the tables.

Example query

Suppose you have two tables, "employees" and "departments," and you want to retrieve a list of all departments along with their corresponding employees. You can use a SQL RIGHT JOIN like this:

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

Example table response

Assuming the "employees" and "departments" tables contain the following data:

employees:

| employee_id | employee_name | department_id |
| ----------- | ------------- | ------------- |
| 1           | John Smith    | 101           |
| 2           | Mary Johnson  | 102           |
| 3           | Sam Brown     | 103           |

departments:

| department_id | department_name |
| ------------- | --------------- |
| 101           | HR              |
| 102           | Finance         |
| 104           | Marketing        |

The query mentioned earlier would return the following result:

| employee_id | employee_name | department_name |
| ----------- | ------------- | --------------- |
| 1           | John Smith    | HR              |
| 2           | Mary Johnson  | Finance         |
| NULL        | NULL          | Marketing        |

This result combines data from both tables, displaying all departments along with their employees. In this case, the department with department_id 104 has no corresponding employees, so the employee_id and employee_name are NULL.

Use cases

  • Retrieving all records from the right table and only matching records from the left table.
  • Handling optional or missing data scenarios.
  • Combining data while preserving all records from one table.

SQL languages this is available for

SQL RIGHT JOIN is a standard SQL feature and is available in most relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Oracle Database
  • SQL Server
  • SQLite
  • IBM Db2
  • MariaDB

The syntax and behavior of RIGHT JOIN are consistent across these database systems.

Related

SQL LEFT JOIN

SQL OUTER JOIN

SQL FULL OUTER JOIN

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.