Loading lessons...
Introduction to Joins
Introduction to Joins
A JOIN combines rows from two or more tables based on a related column between them.
Why join?
Data is split across tables to avoid repeating it. The Customers table has customer info, and the Orders table has order info. A join lets you see both together.
The related column
Customers.CustomerID -> Orders.CustomerID
Orders.CustomerID is a foreign key pointing to the Customers primary key.
The main types of JOIN
| Join | What it returns |
|---|---|
| INNER JOIN | matching rows in both tables |
| LEFT JOIN | all left rows + matching right rows |
| RIGHT JOIN | all right rows + matching left rows |
| FULL OUTER JOIN | all rows from both sides |
The basic shape
SELECT columns
FROM table1
JOIN TYPE table2
ON table1.column = table2.column;
The ON clause says how the tables relate.
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;
TL;DR
- JOIN combines rows from multiple tables.
- The ON clause defines the relationship.
- INNER, LEFT, RIGHT, FULL are the main types.
- Joins use matching columns (usually key columns).