In relational databases, data is often distributed across multiple tables. To retrieve data from these related tables, SQL provides a powerful feature known as joins. SQL joins are used to combine rows from two or more tables based on a related column between them. Joins allow you to query multiple tables at once, making it easier to retrieve meaningful data that spans across different datasets.
In this chapter, we will explore various types of SQL joins, from the basics to more advanced concepts, with examples and explanations of how they work. By the end of this chapter, you will have a deep understanding of how SQL joins operate and how to use them effectively.
A SQL Join is a query operation used to combine data from two or more tables into a single result set. The join is performed based on a related column between these tables, typically using a primary key in one table and a foreign key in another.
There are several types of joins in SQL:
Each type of join serves a specific purpose, depending on how you want to combine the data.
Before we dive into joins, it’s important to understand that tables in a relational database are often related through primary and foreign keys.
For example, you may have:
customers
table with a customer_id
as the primary key.orders
table where customer_id
is a foreign key, relating orders to the customer who placed them.The INNER JOIN keyword selects records that have matching values in both tables. It returns rows where there is at least one match in both tables.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
Suppose you have two tables:
customer_id | customer_name |
---|---|
1 | Alice |
2 | Bob |
3 | Charlie |
order_id | customer_id | order_total |
---|---|---|
101 | 1 | 200 |
102 | 2 | 150 |
SELECT customers.customer_name, orders.order_total
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;
customer_name | order_total |
---|---|
Alice | 200 |
Bob | 150 |
customer_id
in both the customers
and orders
tables. Since Charlie
has no order, he is not included in the result.The LEFT JOIN returns all records from the left table and the matched records from the right table. If there is no match, the result is NULL
for columns from the right table.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
SELECT customers.customer_name, orders.order_total
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
customer_name | order_total |
---|---|
Alice | 200 |
Bob | 150 |
Charlie | NULL |
customers
table and matches them with records from the orders
table. Since Charlie
doesn’t have an order, his order_total
is NULL
.The RIGHT JOIN returns all records from the right table and the matched records from the left table. If there is no match, the result is NULL
for columns from the left table.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
SELECT customers.customer_name, orders.order_total
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
customer_name | order_total |
---|---|
Alice | 200 |
Bob | 150 |
orders
table and matches them with records from the customers
table. If a customer had no orders, their customer_name
would appear as NULL
. Since all orders have a matching customer, no NULL
values are returned in this example.The FULL JOIN returns all records when there is a match in either the left or right table. If there is no match, the result is NULL
for the non-matching side.
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
SELECT customers.customer_name, orders.order_total
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;
customer_name | order_total |
---|---|
Alice | 200 |
Bob | 150 |
Charlie | NULL |
NULL
for the unmatched records.The CROSS JOIN returns the Cartesian product of both tables. This means that every row from the first table is combined with every row from the second table.
SELECT columns
FROM table1
CROSS JOIN table2;
SELECT customers.customer_name, products.product_name
FROM customers
CROSS JOIN products;
customer_name | product_name |
---|---|
Alice | Laptop |
Alice | Phone |
Bob | Laptop |
Bob | Phone |
A Self Join is when a table is joined with itself. It can be useful when you need to compare rows in the same table.
Suppose we have an employees
table where we want to find employees and their managers.
employee_id | employee_name | manager_id |
---|---|---|
1 | Alice | NULL |
2 | Bob | 1 |
3 | Charlie | 1 |
4 | David | 2 |
SELECT e1.employee_name AS Employee, e2.employee_name AS Manager
FROM employees e1
LEFT JOIN employees e2 ON e1.manager_id = e2.employee_id;
Employee | Manager |
---|---|
Alice | NULL |
Bob | Alice |
Charlie | Alice |
David | Bob |
employees
table with itself to find out each employee’s manager. e1
and e2
are aliases for the same table, allowing us to compare employees with their managers.You can also combine multiple join conditions to refine your results.
SELECT c.customer_name, o.order_id, o.order_total
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
AND o.order_total > 100;
customer_name | order_id | order_total |
---|---|---|
Alice | 101 | 200 |
AND
condition refines the join, adding another filter on the order_total
column.Joins are fundamental to working with relational databases. They allow you to combine data from multiple tables, providing more meaningful insights. Understanding the different types of joins—INNER, LEFT, RIGHT, FULL, and CROSS—gives you the flexibility to query your data in powerful ways. Happy coding !❤️