SQL Joins Explained

A Comprehensive Guide

Photo by Ben Wicks on Unsplash

In the world of data analysis, it’s rare to find all the information you need neatly contained in a single table. More often than not, data is spread across multiple tables, linked by common columns. This is where SQL joins come into play. Joins allow you to combine data from these related tables, giving you a complete and unified view of your information. This article serves as a comprehensive guide to SQL joins, covering all the major types with real-world examples to solidify your understanding.

Understanding Relational Databases

Before diving into joins, it’s crucial to grasp the basic concept of relational databases. These databases organize data into multiple tables, each focusing on a specific entity or concept. The relationships between these tables are established through common columns, often referred to as foreign keys. This structure ensures data integrity and reduces redundancy.

Types of SQL Joins

SQL offers several types of joins, each serving a specific purpose in combining data:

INNER JOIN

The most common type of join, INNER JOIN, retrieves only the rows that have matching values in both tables being joined. It’s like finding the intersection of two sets.

Example:

Let’s say we have two tables: Customers and Orders.

Customers Table:

| CustomerID |    Name    |   City   |
|------------|------------|----------|
| 1 | John Doe | New York |
| 2 | Jane Smith | London |
| 3 | David Lee | Paris |

Orders Table:

+---------+------------+--------+
| OrderID | CustomerID | Amount |
+---------+------------+--------+
| 101 | 1 | 100 |
| 102 | 2 | 200 |
| 103 | 1 | 150 |
+---------+------------+--------+

Query

SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:


+------------+---------+--------+
| Name | OrderID | Amount |
+------------+---------+--------+
| John Doe | 101 | 100 |
| Jane Smith | 102 | 200 |
| John Doe | 103 | 150 |
+------------+---------+--------+

The query returns only the customers who have placed orders and their corresponding order details. David Lee is not included because he doesn’t have any orders in the Orders table.

LEFT JOIN

A LEFT JOIN retrieves all rows from the “left” table (the one specified before LEFT JOIN) and the matching rows from the “right” table. If there’s no match in the right table, it returns NULL values for its columns.

Example:

Using the same Customers and Orders tables:

Query:

SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:


+------------+---------+--------+
| Name | OrderID | Amount |
+------------+---------+--------+
| John Doe | 101 | 100 |
| Jane Smith | 102 | 200 |
| David Lee | NULL | NULL |
| John Doe | 103 | 150 |
+------------+---------+--------+

Explanation:

This time, all customers are included in the result. David Lee, who has no orders, appears with NULL values for OrderIDand Amount.

RIGHT JOIN

A RIGHT JOIN is the mirror image of a LEFT JOIN. It retrieves all rows from the “right” table and the matching rows from the “left” table. NULL values are returned for the left table columns if there’s no match.

Example:

Again, using the Customers and Orders tables:

Query:

SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:

|    Name    | OrderID | Amount |
+------------+---------+--------+
| John Doe | 101 | 100 |
| Jane Smith | 102 | 200 |
| John Doe | 103 | 150 |
+------------+---------+--------+

Explanation:

In this case, the result is the same as the INNER JOIN because all orders in the Orders table have a matching customer in the Customers table. If there were an order with a CustomerID not present in the Customers table, that order would be included with a NULL value for the Name.

FULL OUTER JOIN

A FULL OUTER JOIN retrieves all rows from both tables. If there’s no match in one table, it returns NULL values for the corresponding columns. It’s essentially a combination of LEFT JOIN and RIGHT JOIN.

Example:

Let’s add a new order to the Orders table with a CustomerID that doesn’t exist in the Customers table:

Orders Table:

| OrderID | CustomerID | Amount |
+---------+------------+--------+
| 101 | 1 | 100 |
| 102 | 2 | 200 |
| 103 | 1 | 150 |
| 104 | 4 | 300 |
+---------+------------+--------+

Query:

SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Result:

|    Name    | OrderID | Amount |
+------------+---------+--------+
| John Doe | 101 | 100 |
| Jane Smith | 102 | 200 |
| David Lee | NULL | NULL |
| John Doe | 103 | 150 |
| NULL | 104 | 300 |
+------------+---------+--------+

Explanation:

Now, the result includes all customers and all orders. David Lee is included with NULL values for order details, and the new order (OrderID 104) is included with a NULL value for the customer name.

SQL joins are indispensable tools for data professionals working with relational databases. Understanding the different types of joins and their applications empowers you to extract meaningful insights from your data. By mastering INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, you can effectively combine data from multiple tables, unravel complex relationships, and gain a comprehensive view of your information landscape. Remember to choose the appropriate join type based on the specific needs of your analysis, ensuring you retrieve the precise data required for your queries.


SQL Joins Explained was originally published in Data Decoded on Medium, where people are continuing the conversation by highlighting and responding to this story.

Scroll to Top