Contents
SQL Joins Explained
Good databases split information across several tables instead of repeating it everywhere. Client details sit in one table, and projects sit in another. A join is how you bring them back together in a single result by matching a shared column such as client_id.
The join type decides what happens to rows that find no match on the other side, and that is the part beginners usually find confusing.
The Two Tables
Here are clients and projects for a small design agency. Notice two things. Meadow Fitness has no projects yet, and the Brand Audit project points to client 9, which doesn't exist in the clients table.
clients
client_id | client_name |
| 1 | Orion Labs |
| 2 | Bluebird Foods |
| 3 | Kiran Studio |
| 4 | Meadow Fitness |
projects
project_id | client_id | project_name | budget |
| 101 | 1 | Website Revamp | 90000 |
| 102 | 1 | Mobile App | 150000 |
| 103 | 2 | Logo Design | 25000 |
| 104 | 3 | Ad Campaign | 60000 |
| 105 | 9 | Brand Audit | 40000 |
INNER JOIN
INNER JOIN keeps only the rows that have a match on both sides. Anything without a partner is dropped.
SELECT c.client_name, p.project_name, p.budget
FROM clients AS c
INNER JOIN projects AS p
ON c.client_id = p.client_id;Output:
client_name | project_name | budget |
| Orion Labs | Website Revamp | 90000 |
| Orion Labs | Mobile App | 150000 |
| Bluebird Foods | Logo Design | 25000 |
| Kiran Studio | Ad Campaign | 60000 |
Code Explanation:
- c and p are short aliases, which save typing and make it clear which table each column comes from.
- ON c.client_id = p.client_id is the matching rule. A client row and a project row are paired only when these two values are equal.
- Orion Labs appears twice because it has two projects, and each match becomes its own row.
- Meadow Fitness and the Brand Audit project are missing because neither found a partner.
LEFT JOIN
LEFT JOIN keeps every row from the first (left) table, even when nothing matches. The missing side is filled with NULL.
SELECT c.client_name, p.project_name
FROM clients AS c
LEFT JOIN projects AS p
ON c.client_id = p.client_id;Output:
client_name | project_name |
| Orion Labs | Website Revamp |
| Orion Labs | Mobile App |
| Bluebird Foods | Logo Design |
| Kiran Studio | Ad Campaign |
| Meadow Fitness | NULL |
Code Explanation:
- We keep every client because clients is on the left side of the join.
- Meadow Fitness has no project, so project_name comes back as NULL.
- The orphan Brand Audit project is still missing, because it belongs to the right table and has no left-side match.
A handy trick follows from this. Add WHERE p.project_id IS NULL to the query above, and you get only the clients with no projects at all. This is a common way to find data gaps.
How Each SQL Join Type Handles Unmatched Rows
Join Type | Rows Kept | Meadow Fitness | Brand Audit |
| INNER JOIN | Matches only | Dropped | Dropped |
| LEFT JOIN | All left rows, plus matches | Kept with NULLs | Dropped |
| RIGHT JOIN | All right rows, plus matches | Dropped | Kept with NULLs |
| FULL JOIN | All rows from both sides | Kept with NULLs | Kept with NULLs |
MySQL does not support FULL JOIN. The usual workaround is to combine a LEFT JOIN and a RIGHT JOIN with UNION.
SQL for Data Science
Abhishek Pati








