SQL JOINs
Combine related tables on a matching key
Relational databases split data across tables — employees here, departments there — connected by keys. A JOIN stitches rows from two tables wherever the keys match: INNER JOIN keeps only matches, LEFT JOIN keeps all left-side rows (NULL-filling missing matches), RIGHT/FULL mirror the idea. This is SQL’s superpower: normalised storage, arbitrary combinations at query time.
Two tables, one key
employees carries department; departments carries dept_id — the shared key is the join's bridge.
Pick the join type
INNER = matches only; LEFT = every left row, NULL-padded where no match; RIGHT mirrors; FULL OUTER keeps both sides.
Name the bridge in ON
ON employees.department = departments.dept_id — the engine pairs rows via this condition.
Wide rows come out
Each output row glues an employee row to its department row — columns from both tables side by side.
Alias and prefix
FROM employees e JOIN departments d ON e.department = d.dept_id — short aliases keep wide queries readable and unambiguous.
1SELECT e.name, e.salary, d.location, d.head2FROM employees e3INNER JOIN departments d4 ON e.department = d.dept_id;
| name | salary | location | head |
|---|---|---|---|
| Alice | 85000 | Berlin | Cynthia |
| Bob | 52000 | Madrid | Elena |
| Cynthia | 91000 | Berlin | Cynthia |
| David | 47000 | Lisbon | David |
| Elena | 58000 | Madrid | Elena |
| Farid | 73000 | Berlin | Cynthia |
Every employee row reaches across to its department row via the shared key, producing one wide row each. Switch to LEFT JOIN in the visualization and imagine a new “Research” department with nobody in it — it would appear with NULL employee columns.
INNER JOIN
Only rows with a match on both sides.
1SELECT e.name, e.salary, d.location, d.head2FROM employees e3INNER JOIN departments d4 ON e.department = d.dept_id;
| name | salary | location | head |
|---|---|---|---|
| Alice | 85000 | Berlin | Cynthia |
| Bob | 52000 | Madrid | Elena |
| Cynthia | 91000 | Berlin | Cynthia |
| David | 47000 | Lisbon | David |
| Elena | 58000 | Madrid | Elena |
| Farid | 73000 | Berlin | Cynthia |
LEFT JOIN
Keep every left row; NULL-fill missing matches.
1SELECT d.dept_id, e.name2FROM departments d3LEFT JOIN employees e4 ON e.department = d.dept_id5ORDER BY d.dept_id;
| dept_id | name |
|---|---|
| Engineering | Alice |
| Engineering | Cynthia |
| Engineering | Farid |
| Marketing | David |
| Sales | Bob |
| Sales | Elena |
JOIN + GROUP BY
The classic analytics combo.
1SELECT d.location, COUNT(*) AS staff,2 ROUND(AVG(e.salary), 0) AS avg_salary3FROM employees e4JOIN departments d ON e.department = d.dept_id5GROUP BY d.location;
| location | staff | avg_salary |
|---|---|---|
| Berlin | 3 | 83000 |
| Madrid | 2 | 55000 |
| Lisbon | 1 | 47000 |
Fan-out warning
Duplicate keys multiply rows.
1SELECT e.name, d.head2FROM employees e3JOIN departments d ON e.department = d.dept_id4WHERE e.department = 'Engineering';
| name | head |
|---|---|
| Alice | Cynthia |
| Cynthia | Cynthia |
| Farid | Cynthia |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.