Structured Query Language (SQL) is a powerful tool used by developers and data analysts to work with relational databases. One of the most important operations in SQL is the join. Joining tables allows you to retrieve data from multiple tables based on a related column. In this article, we’ll explore the different types of joins in SQL, provide examples, and dive into their inner workings.
Types of SQL Joins
There are four main types of joins in SQL:
- Inner Join
- Outer Join
- Left Outer Join
- Right Outer Join
- Full Outer Join
- Cross Join
- Self Join
Let’s go through each of them in detail.
1. Inner Join
The most common type of join is the inner join, which returns only the rows that have matching values in both tables.
Suppose we have two tables, orders
and customers
, with the following data:
orders
order_id | customer_id | product
--------------------------------
1 | 1 | Apples
2 | 2 | Bananas
3 | 3 | Grapes
customers
customer_id | customer_name
---------------------------
1 | Alice
2 | Bob
3 | Charlie
To get a list of orders along with their customer names, we can perform an inner join on the customer_id
column:
SELECT orders.order_id, orders.product, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id;
This would return the following result:
order_id | product | customer_name
----------------------------------
1 | Apples | Alice
2 | Bananas | Bob
3 | Grapes | Charlie
2. Outer Join
Outer joins return not only the rows with matching values in both tables but also unmatched rows from one or both tables. There are three types of outer joins:
2.1 Left Outer Join
A left outer join returns all rows from the left table and the matched rows from the right table. If there’s no match, NULL values are returned for columns from the right table.
Let’s say we want to find all customers and their orders, even if they haven’t placed an order. We can perform a left outer join:
SELECT customers.customer_name, orders.order_id, orders.product
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;
Result:
customer_name | order_id | product
-----------------------------------
Alice | 1 | Apples
Bob | 2 | Bananas
Charlie | 3 | Grapes
2.2 Right Outer Join
A right outer join returns all rows from the right table and the matched rows from the left table. If there’s no match, NULL values are returned for columns from the left table.
SELECT customers.customer_name, orders.order_id, orders.product
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;
Result:
customer_name | order_id | product
-----------------------------------
Alice | 1 | Apples
Bob | 2 | Bananas
Charlie | 3 | Grapes
2.3 Full Outer Join
A full outer join returns all rows from both tables, with matched rows from both sides, and unmatched rows from either side filled with NULL values.
SELECT customers.customer_name, orders.order_id, orders.product
FROM customers
FULL OUTER JOIN orders ON customers.customer_id = orders.customer_id;
Result:
customer_name | order_id | product
-----------------------------------
Alice | 1 | Apples
Bob | 2 | Bananas
Charlie | 3 | Grapes
3. Cross Join
A cross join (also known as a Cartesian join) returns the Cartesian product of the two tables, meaning each row from the first table is combined with each row from the second table.
SELECT customers.customer_name, orders.product
FROM customers
CROSS JOIN orders;
Result:
customer_name | product
-----------------------
Alice | Apples
Alice | Bananas
Alice | Grapes
Bob | Apples
Bob | Bananas
Bob | Grapes
Charlie | Apples
Charlie | Bananas
Charlie | Grapes
4. Self Join
A self join is a join of a table with itself, using aliases to differentiate between the original table and its copy. This is useful when dealing with hierarchical data or comparing rows within the same table.
Suppose we have an employees
table with the following data:
employee_id | employee_name | manager_id
----------------------------------------
1 | John | NULL
2 | Jane | 1
3 | Jim | 1
4 | Jill | 2
To find employees and their managers, we can perform a self join:
SELECT e1.employee_name AS employee, e2.employee_name AS manager
FROM employees e1
JOIN employees e2 ON e1.manager_id = e2.employee_id;
Result:
employee | manager
------------------
Jane | John
Jim | John
Jill | Jane
In conclusion, understanding how SQL joins work and their differences is crucial for effectively retrieving data from relational databases. With this guide and the provided examples, you should be well-equipped to tackle any join-related task!