Quick answer
A SQL join combines rows from two tables using a related column, often called a key. Joins are how you take data that is stored separately and bring it together in one result.
In beginner terms, a join answers questions like:
- Which customers placed which orders?
- Which employees belong to which departments?
- Which products have sales records and which do not?
In this guide
Why joins exist
In real databases, information is usually split across multiple tables instead of crammed into one giant table.
For example:
- A
customerstable stores customer names - An
orderstable stores purchases - A
departmentstable stores department details
This design keeps data cleaner and avoids repeating the same values over and over. But it also means you need joins to see related data together.
Sample tables
Suppose you have these two tables:
customers
| customer_id | name |
|---|---|
| 1 | Ana |
| 2 | Ben |
| 3 | Chris |
| 4 | Dana |
orders
| order_id | customer_id | amount |
|---|---|---|
| 101 | 1 | 50 |
| 102 | 2 | 75 |
| 103 | 2 | 20 |
orders table. That matters a lot when you choose a join type.
What the join key does
A join works because both tables share a related column. In this example, that column is customer_id.
ON c.customer_id = o.customer_id
That line tells SQL how rows in one table connect to rows in the other table.
If the join key is wrong, your results will be wrong. This is one of the most common beginner mistakes.
INNER JOIN
INNER JOIN returns only rows where the join condition matches in both tables.
SELECT c.customer_id, c.name, o.order_id, o.amount FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id;
This query returns:
| customer_id | name | order_id | amount |
|---|---|---|---|
| 1 | Ana | 101 | 50 |
| 2 | Ben | 102 | 75 |
| 2 | Ben | 103 | 20 |
LEFT JOIN
LEFT JOIN returns all rows from the left table, plus matching rows from the right table.
SELECT c.customer_id, c.name, o.order_id, o.amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
This query returns:
| customer_id | name | order_id | amount |
|---|---|---|---|
| 1 | Ana | 101 | 50 |
| 2 | Ben | 102 | 75 |
| 2 | Ben | 103 | 20 |
| 3 | Chris | NULL | NULL |
| 4 | Dana | NULL | NULL |
LEFT JOIN keeps every row from the left table.
Compare the results
NULL values in a LEFT JOIN.
Finding missing matches
One of the most useful LEFT JOIN patterns is finding rows that do not have a match in another table.
SELECT c.customer_id, c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;
This returns customers with no orders.
When to use each join
Use INNER JOIN when:
- you only care about confirmed matches
- unmatched rows should be excluded
- you want related records only
Use LEFT JOIN when:
- you need all rows from the main table
- you want to preserve missing relationships
- you are looking for unmatched rows
Common mistakes
- Joining on the wrong columns
- Using
INNER JOINwhen you actually needed to keep unmatched rows - Forgetting that a LEFT JOIN can produce
NULLvalues on the right side - Adding a
WHEREfilter that accidentally removes unmatched LEFT JOIN rows - Assuming one row in one table always matches one row in the other table
LEFT JOIN. Some filters can accidentally make the result behave more like an INNER JOIN.
Practice questions
Bottom line
Joins connect tables. They let you take related data that lives in separate places and see it together in one query result.
Start by mastering INNER JOIN and LEFT JOIN. Those two join types cover a huge share of real beginner and intermediate SQL work.
Related SQL tutorials
Start here for all SQL topics and tutorials.
See a more direct side-by-side comparison of the most common join types.
Learn how to summarize grouped data after you join tables together.
Understand COUNT, SUM, AVG, MIN, and MAX for reporting and analysis.