SQL Joins Explained: A Comprehensive Guide

Welcome to our detailed guide to SQL Joins! No matter your level of familiarity with SQL, we aim to provide a thorough understanding of the different types of SQL Joins, why they are important, and how to use them.

What are SQL Joins?

link to this section

SQL Joins are used to combine rows from two or more tables based on a related column between them. It allows you to merge data from multiple tables into a single result, which can then be used to provide insightful data to business analysts or other stakeholders.

Types of Joins in SQL

link to this section

There are four basic types of SQL Joins:

  1. Inner Join: Returns records that have matching values in both tables.

  2. Left (Outer) Join: Returns all records from the left table, and the matched records from the right table.

  3. Right (Outer) Join: Returns all records from the right table, and the matched records from the left table.

  4. Full (Outer) Join: Returns all records when there is a match in either the left or the right table.

Let's go through each of these in detail.

Inner Join

The INNER JOIN keyword selects records that have matching values in both tables. Let's consider two tables, Orders and Customers , and we want to find all orders made by each customer.

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

This SQL statement would return the OrderID and the CustomerName for all orders where the CustomerID in Orders matches the CustomerID in Customers .

Left (Outer) Join

The LEFT JOIN keyword returns all records from the left table ( table1 ), and the matched records from the right table ( table2 ). The result is NULL from the right side, if there is no match.

Let's consider the same Orders and Customers tables, and we want to find all customers and their respective orders. But we also want to find all customers who have not placed any order.

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

This SQL statement would return all the customers along with their OrderID , even if they have not placed any order.

Right (Outer) Join

The RIGHT JOIN keyword returns all records from the right table ( table2 ), and the matched records from the left table ( table1 ). The result is NULL from the left side, when there is no match.

Consider the Orders and Customers tables, and we want to find all orders and the customers who placed them. But we also want to find all orders that aren't associated with a customer.

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

This SQL statement would return all the orders along with the CustomerName , even if they are not placed by any customer.

Full (Outer) Join

The FULL OUTER JOIN keyword returns all records when there is a match in either left ( table1 ) or right ( table2 ) table records.

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

This SQL statement would return all CustomerName and OrderID , and it would return NULL in either column if there isn't a match.

Self Join

link to this section

A self join is a regular join where a table is joined with itself. This can be useful when the data being compared is within the same table.

For example, consider a table Employees that includes a field ManagerID which references the EmployeeID of the employee's manager in the same table. A self join can help to pair each employee with their respective manager's record:

SELECT E1.EmployeeName AS Employee, E2.EmployeeName AS Manager 
FROM Employees E1 
JOIN Employees E2 
ON E1.ManagerID = E2.EmployeeID; 

In this query, we've aliased the Employees table as E1 and E2 to treat them as separate entities in the join.

Cartesian Product or Cross Join

link to this section

In SQL, a Cartesian Product or Cross Join returns a result set that is the product of rows of two joined tables when no WHERE clause is used with SQL JOIN. This type of join returns all possible combinations of rows from the joined tables. It's generally used when there are no matching columns between tables.

SELECT Customers.CustomerName, Products.ProductName 
FROM Customers 
CROSS JOIN Products; 

This query would return a combination of every customer paired with every product, which can be useful in certain scenarios like generating all possible combinations of items.

Non-Equi Joins

link to this section

Non-equi joins are joins that use comparison operators other than equalities. While not as commonly used as the previous joins, there are scenarios where non-equi joins can be extremely useful.

For instance, consider a table Discounts with fields MinPurchase and DiscountRate , and a table Orders with OrderID and PurchaseAmount . A non-equi join could be used to find the applicable discount for each order:

SELECT Orders.OrderID, Discounts.DiscountRate 
FROM Orders 
LEFT JOIN Discounts 
ON Orders.PurchaseAmount >= Discounts.MinPurchase 

Handling NULL Values in Joins

link to this section

There are occasions where one or more columns in a table may not have a value (NULL). In such cases, it's important to handle these null values appropriately during the join operations.

For instance, if you perform an INNER JOIN on two tables with NULL values, these rows won't be included in the result set, because NULL does not equal anything, including other NULLs. If you want to include those rows in your results, you might consider using a LEFT JOIN, RIGHT JOIN, or FULL OUTER JOIN, depending on the requirements.

Using Joins with Aggregate Functions

link to this section

SQL Joins can be used in conjunction with aggregate functions (SUM, COUNT, AVG, MAX, MIN) to derive meaningful data from joined tables. For example, you might want to find the total quantity of all products sold. If you have a Sales table and a Products table, you could use an INNER JOIN to join the two tables and the SUM function to add up the quantities:

SELECT Products.ProductName, SUM(Sales.Quantity) as TotalQuantity 
FROM Sales 
INNER JOIN Products 
ON Sales.ProductID = Products.ProductID 
GROUP BY Products.ProductName; 

This will give you the total quantity of each product sold.

Conclusion

link to this section

SQL Joins are incredibly powerful and provide the ability to extract significant insights from relational databases. They form the backbone of data manipulation and querying in SQL. By understanding and applying the different types of joins, you can make your data work effectively for you. As with many things, practice makes perfect, so don't be afraid to get hands-on and try these out with your own data!