PostgreSQL: Using RIGHT JOIN to join multiple tables

Updated: January 5, 2024 By: Guest Contributor Post a comment

Overview

In PostgreSQL, understanding how to effectively combine data from multiple tables is crucial for complex queries. This tutorial explores the RIGHT JOIN operation, highlighting how it can be used to retrieve inclusive data sets across multiple tables, with practical examples to consolidate your learning.

Introduction to RIGHT JOIN

The RIGHT JOIN clause in PostgreSQL is a type of OUTER JOIN that’s particularly useful when you’re interested in retaining all rows from the ‘right’ table in a query, regardless of whether there are matching rows in the ‘left’ table. It’s the opposite of a LEFT JOIN, where all rows from the ‘left’ table are kept. When using a RIGHT JOIN, the result set will include all the records from the right table, even if the ON condition does not find any matching record in the left table. If there is no match, the result is NULL on the side of the table where the data is missing.

For our examples, assume the following table structures and relationships:

-- Table: employees
CREATE TABLE employees (
  employee_id SERIAL PRIMARY KEY,
  name VARCHAR(100),
  department_id INT
);

-- Table: departments
CREATE TABLE departments (
  department_id SERIAL PRIMARY KEY,
  department_name VARCHAR(50)
);

Basic Example of RIGHT JOIN

Here’s a simple query using RIGHT JOIN:

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

This will produce a list of all department names and employee names, including departments that have no employees associated with them. Departments without associated employees will show NULL for employee names.

RIGHT JOIN with WHERE condition

Incorporating a WHERE condition can filter the results further:

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id
WHERE departments.department_name = 'Sales';

This will display only those employees who belong to the ‘Sales’ department, as well as include the ‘Sales’ department even if it has no employees.

RIGHT JOIN Multiple Tables

Joining more than two tables with RIGHT JOIN becomes complex but follows the same principles:

-- Table: projects
CREATE TABLE projects (
  project_id SERIAL PRIMARY KEY,
  project_name VARCHAR(100),
  lead_id INT
);

SELECT 
  employees.name AS employee_name,
  departments.department_name,
  projects.project_name
FROM
  departments
LEFT JOIN employees ON employees.department_id = departments.department_id
RIGHT JOIN projects ON employees.employee_id = projects.lead_id;

Here, the RIGHT JOIN ensures all projects are listed along with their lead employee names and department names, with NULL in their place if there is no associated employee or department.

RIGHT JOIN with Aggregate Functions

RIGHT JOIN works with aggregate functions to perform calculations over groups of data:

SELECT 
  departments.department_name,
  COUNT(employees.employee_id) AS employee_count
FROM
  departments
RIGHT JOIN employees ON employees.department_id = departments.department_id
GROUP BY departments.department_name;

This will list each department and the count of employees in that department. However, due to the RIGHT JOIN, it will also include departments with 0 employees.

Advanced Usage of RIGHT JOIN

In more complex scenarios, RIGHT JOIN can involve subqueries, CASE statements, and other advanced SQL techniques. For the sake of brevity, these examples will not be covered in this tutorial.

Conclusion

This tutorial provided a wide range of examples demonstrating the utility of the RIGHT JOIN clause in PostgreSQL. While not as commonly used as LEFT JOIN, RIGHT JOIN offers a unique function in queries where completeness of the ‘right’ data set is required. With the examples provided, you should now have a firm grasp on using RIGHT JOIN in your PostgreSQL queries.