Introduction to JOINs
Understand how tables relate to each other
JOINs combine rows from two or more tables based on related columns. They're fundamental to working with relational databases.
Why Use JOINs?
In relational databases, data is split across multiple tables to avoid duplication. JOINs reunite this data when you need it.
Example Tables
users
| id | name | |
|---|---|---|
| 1 | Alice | alice@mail.com |
| 2 | Bob | bob@mail.com |
| 3 | Carol | carol@mail.com |
orders
| id | user_id | total |
|---|---|---|
| 101 | 1 | $150 |
| 102 | 1 | $75 |
| 103 | 2 | $200 |
The Relationship
The user_id column in orders references the id column in users. This is called a foreign key relationship.
JOIN Types Overview
| Type | Returns |
|---|---|
| INNER JOIN | Only matching rows from both tables |
| LEFT JOIN | All rows from left + matches from right |
| RIGHT JOIN | All rows from right + matches from left |
| FULL OUTER JOIN | All rows from both tables |
| CROSS JOIN | Every combination (Cartesian product) |
Note: JOIN without a prefix defaults to INNER JOIN.