When working with relational databases, data is often stored in separate tables to maintain normalization. However, to extract meaningful information, you might need to combine data from multiple tables. SQL JOIN operations are essential for this purpose, and they allow you to retrieve related data from multiple tables in a single query.
Before diving into examples of joining three tables, let's quickly review how SQL JOINs work.
SQL JOIN operations combine rows from two or more tables based on a related column. The most commonly used types of JOINs are:
NULL
values are returned for columns from the right table.NULL
values are returned for missing matches.For this example, we will work with three tables:
Here’s a brief overview of the three tables we'll use in our examples:
customer_id | name | |
---|---|---|
1 | John Doe | john@example.com |
2 | Jane Smith | jane@example.com |
3 | Bob Johnson | bob@example.com |
order_id | customer_id | product_id | order_date |
---|---|---|---|
1001 | 1 | 101 | 2024-12-01 |
1002 | 2 | 102 | 2024-12-05 |
1003 | 1 | 103 | 2024-12-07 |
product_id | product_name | price |
---|---|---|
101 | Laptop | 1000 |
102 | Smartphone | 500 |
103 | Tablet | 300 |
The INNER JOIN returns records that have matching values in both tables. If a customer has placed an order, the query will return their information along with the product details.
SELECT
customers.name,
orders.order_id,
products.product_name,
products.price
FROM
customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
INNER JOIN products ON orders.product_id = products.product_id;
customers
table with the orders
table on the customer_id
column.orders
table with the products
table on the product_id
column.name | order_id | product_name | price |
---|---|---|---|
John Doe | 1001 | Laptop | 1000 |
Jane Smith | 1002 | Smartphone | 500 |
John Doe | 1003 | Tablet | 300 |
The LEFT JOIN ensures all customers are included, even if they haven't placed any orders. For customers who haven't ordered anything, the product details will be NULL
.
SELECT
customers.name,
orders.order_id,
products.product_name,
products.price
FROM
customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
LEFT JOIN products ON orders.product_id = products.product_id;
customers
and orders
, ensuring that all orders are included, even if there’s no matching customer.name | order_id | product_name | price |
---|---|---|---|
John Doe | 1001 | Laptop | 1000 |
Jane Smith | 1002 | Smartphone | 500 |
John Doe | 1003 | Tablet | 300 |
A FULL OUTER JOIN combines the results of both LEFT JOIN and RIGHT JOIN. It returns all rows when there is a match in one of the tables. If there’s no match, NULL
values are returned for missing matches.
SELECT
customers.name,
orders.order_id,
products.product_name,
products.price
FROM
customers
FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id
FULL OUTER JOIN products ON orders.product_id = products.product_id;
name | order_id | product_name | price |
---|---|---|---|
John Doe | 1001 | Laptop | 1000 |
Jane Smith | 1002 | Smartphone | 500 |
John Doe | 1003 | Tablet | 300 |
Bob Johnson | NULL | NULL | NULL |
Use Aliases for Simplicity: When dealing with multiple tables, using table aliases (shortened names) can make your query easier to read.
Example:
SELECT
c.name,
o.order_id,
p.product_name,
p.price
FROM
customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN products p ON o.product_id = p.product_id;
Ensure Proper Relationships: Make sure your tables are properly related through foreign keys (e.g., customer_id
in orders
table refers to customers
table). This ensures the correctness of your JOIN results.
Limit Your Results: If you expect a large result set, always use LIMIT
or TOP
to reduce the size of the data returned.