Explain joins in SQL with examples.
Of course. Here is a comprehensive explanation of SQL JOIN
clauses, complete with their purpose, syntax, visual analogies, and practical examples.
A JOIN
clause in SQL is used to combine rows from two or more tables based on a related column between them.
Databases are typically normalized, which means data is split into multiple, smaller tables to reduce redundancy and improve data integrity. For example, instead of storing customer information with every single order, you have a Customers
table and an Orders
table. JOIN
s are the essential tool for bringing this related, separated data back together in a meaningful way for queries and reports.
To demonstrate the different types of joins, we'll use two simple tables: Customers
and Orders
.
Customers
Table:
| CustomerID | Name | City |
| :--- | :--- | :--- |
| 1 | Alice | New York |
| 2 | Bob | London |
| 3 | Charlie | Paris |
Orders
Table:
| OrderID | CustomerID | OrderDate | Amount |
| :--- | :--- | :--- | :--- |
| 101 | 1 | 2023-01-15 | 150.00 |
| 102 | 2 | 2023-02-10 | 200.00 |
| 103 | 99 | 2023-02-18 | 50.00 |
Notice a few key things about our data:
Alice and Bob are in the Customers
table and have placed orders in the Orders
table.
Charlie is in the Customers
table but has no orders.
* Order 103 is in the Orders
table but has a CustomerID
(99) that does not exist in the Customers
table (this could be old or bad data).
Here are the most common types of joins. We can visualize them using Venn diagrams, where each circle represents a table.
INNER JOIN
`
sql`
Example: Get a list of all orders with the corresponding customer names.
`
sql
SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
`
CustomerID
s in both tables. Charlie (no orders) and the orphan order (no customer) are excluded.LEFT JOIN
(or LEFT OUTER JOIN
)NULL
.`
sql`
Example: Get a list of all customers and any orders they may have placed.
`
sql
SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
`
Orders
table are NULL
for him. This is useful for finding customers who haven't placed an order.RIGHT JOIN
(or RIGHT OUTER JOIN
)NULL
.`
sql`
Example: Get a list of all orders and the customer who placed them, if any.
`
sql
SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
`
Customers
table are NULL
. This is useful for finding orders with invalid or missing customer data.FULL OUTER JOIN
LEFT JOIN
and a RIGHT JOIN
.`
sql`
Example: Get a complete list of all customers and all orders, linking them where possible.
`
sql
SELECT Customers.Name, Orders.OrderID, Orders.Amount
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
`
FULL OUTER JOIN
command. You can simulate it by combining a LEFT JOIN
and a RIGHT JOIN
with a UNION
operator.)CROSS JOIN
ON
clause.`
sql`
Example: Match every customer with every possible order.
`
sql
SELECT Customers.Name, Orders.OrderID
FROM Customers
CROSS JOIN Orders;
`