Understanding SQL Joins: Left Join, Right Join, and Outer Join

27 May 2025
5 min read

Introduction

When working with SQL databases, joining tables is a fundamental task that allows you to combine data from multiple sources into a single result set. Among the various types of joins, Left Join, Right Join, and Outer Join are particularly important. Each serves a different purpose and helps you retrieve data in specific ways. In this blog post, we’ll explore these three types of joins to help you understand how and when to use them.

1. Left Join (or Left Outer Join)

Definition: A Left Join returns all records from the left table (Table A) and the matched records from the right table (Table B). If there is no match, NULL values are returned for columns from the right table.

Syntax:

SELECT columns

FROM TableA

LEFT JOIN TableB

ON TableA.key = TableB.key;

Example: Imagine you have two tables: Employees and Departments. The Employees table lists employees and their department IDs, while the Departments table lists departments.

-- Employees table

EmployeeID | Name    | DepartmentID

-----------|---------|--------------

1          | Alice   | 101

2          | Bob     | 102

3          | Charlie | NULL

-- Departments table

DepartmentID | DepartmentName

-------------|---------------

101          | HR

102          | IT

To find all employees and their departments, including those who are not assigned to any department, you would use a Left Join:

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

LEFT JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

Result:

Name    | DepartmentName

--------|---------------

Alice   | HR

Bob     | IT

Charlie | NULL

2. Right Join (or Right Outer Join)

Definition: A Right Join returns all records from the right table (Table B) and the matched records from the left table (Table A). If there is no match, NULL values are returned for columns from the left table.

Syntax:

SELECT columns

FROM TableA

RIGHT JOIN TableB

ON TableA.key = TableB.key;

Example: Using the same Employees and Departments tables, if you want to list all departments and their employees, including departments with no employees, you’d use a Right Join:

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

RIGHT JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

Result:

Name    | DepartmentName

--------|---------------

Alice   | HR

Bob     | IT

NULL    | Marketing

In this example, if the Departments table had a department with no employees, it would still appear in the result set with NULL values for the employee name.

3. Full Outer Join

Definition: A Full Outer Join returns all records when there is a match in either the left table (Table A) or the right table (Table B). If there is no match, NULL values are returned for columns from the table that does not have a matching record.

Syntax:

SELECT columns

FROM TableA

FULL OUTER JOIN TableB

ON TableA.key = TableB.key;

Example: To find all employees and all departments, including employees without departments and departments without employees, you would use a Full Outer Join:

SELECT Employees.Name, Departments.DepartmentName

FROM Employees

FULL OUTER JOIN Departments

ON Employees.DepartmentID = Departments.DepartmentID;

Result:

Name    | DepartmentName

--------|---------------

Alice   | HR

Bob     | IT

Charlie | NULL

NULL    | Marketing

Here, Charlie is listed with NULL for the department name because they are not assigned to any department, and Marketing is listed with NULL for the employee name because there are no employees in that department.

Summary

Left Join: Includes all records from the left table and matched records from the right table. Returns NULLs for non-matching right table records.

Right Join: Includes all records from the right table and matched records from the left table. Returns NULLs for non-matching left table records.

Full Outer Join: Includes all records from both tables, with NULLs where there is no match in one of the tables.

Pixel Perfect Reports support of Left Join, Right Join, and Full Outer Join

By default, the Joins Pixel Perfect Reports generated by the Ancestor and Descendent graphs configured by users when they select the tables in the Ancestor and Descendent graphs are Inner Joins. Users however can change them to Left Joins, Right Joins, or Full Outer Joins simply by checking or unchecking the nodes situated on both ends of the Join. By default these nodes are unchecked, and hence the Joins are Inner Joins. To make a Left Join, check the node located at the upper end of the Join.

Left Join

To make a Right Join, check the node located at the lower end of the Join.

Right Join

To make an Outer Full Join, check both nodes.

Outer Full Join

Conclusion

Understanding these joins helps you craft queries that effectively combine and analyze data from different tables. Experimenting with these joins in your SQL queries will give you a deeper understanding of how they affect your results.  Feel free to explore these joins in your Pixel Perfect Reports, ultimately leading to more insightful and effective reporting.

Share this post