Learn SQL
-- joining.joins

SQL JOINs

Combine related tables on a matching key

--concept

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.

--how it works
1

Two tables, one key

employees carries department; departments carries dept_id — the shared key is the join's bridge.

2

Pick the join type

INNER = matches only; LEFT = every left row, NULL-padded where no match; RIGHT mirrors; FULL OUTER keeps both sides.

3

Name the bridge in ON

ON employees.department = departments.dept_id — the engine pairs rows via this condition.

4

Wide rows come out

Each output row glues an employee row to its department row — columns from both tables side by side.

5

Alias and prefix

FROM employees e JOIN departments d ON e.department = d.dept_id — short aliases keep wide queries readable and unambiguous.

--visualization
JOIN — bare keyword defaults to INNER
employeesAliceEnginBobSalesCynthiaEnginDavidMarkeElenaSalesFaridEngindepartmentsEngineeringBerlinSalesMadridMarketingLisbonON e.department = d.dept_idINNER JOIN
Stage 1 / 8
Two tables, linked by a key: employees.department ↔ departments.dept_id. Join type: INNER JOIN.
--example
query.sql
1SELECT e.name, e.salary, d.location, d.head
2FROM employees e
3INNER JOIN departments d
4 ON e.department = d.dept_id;
Result
namesalarylocationhead
Alice85000BerlinCynthia
Bob52000MadridElena
Cynthia91000BerlinCynthia
David47000LisbonDavid
Elena58000MadridElena
Farid73000BerlinCynthia

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.

--variants

INNER JOIN

Only rows with a match on both sides.

query.sql
1SELECT e.name, e.salary, d.location, d.head
2FROM employees e
3INNER JOIN departments d
4 ON e.department = d.dept_id;
Result
namesalarylocationhead
Alice85000BerlinCynthia
Bob52000MadridElena
Cynthia91000BerlinCynthia
David47000LisbonDavid
Elena58000MadridElena
Farid73000BerlinCynthia
1Each employee reaches across to its department row via the key.
2All six employees find their department — six wide rows.
3A department with no employees would not appear at all.

LEFT JOIN

Keep every left row; NULL-fill missing matches.

query.sql
1SELECT d.dept_id, e.name
2FROM departments d
3LEFT JOIN employees e
4 ON e.department = d.dept_id
5ORDER BY d.dept_id;
Result
dept_idname
EngineeringAlice
EngineeringCynthia
EngineeringFarid
MarketingDavid
SalesBob
SalesElena
1The left table drives: every department appears at least once.
2All three departments have staff here, so no NULL rows appear — every match exists.
3If a "Research" department existed with nobody in it, it would still appear — once, with NULL name. Find such orphans with WHERE e.name IS NULL.

JOIN + GROUP BY

The classic analytics combo.

query.sql
1SELECT d.location, COUNT(*) AS staff,
2 ROUND(AVG(e.salary), 0) AS avg_salary
3FROM employees e
4JOIN departments d ON e.department = d.dept_id
5GROUP BY d.location;
Result
locationstaffavg_salary
Berlin383000
Madrid255000
Lisbon147000
1Join first: employees gain their office location.
2Then group by the joined column and aggregate.
3Reports like this are why JOIN and GROUP BY are taught together.

Fan-out warning

Duplicate keys multiply rows.

query.sql
1SELECT e.name, d.head
2FROM employees e
3JOIN departments d ON e.department = d.dept_id
4WHERE e.department = 'Engineering';
Result
namehead
AliceCynthia
CynthiaCynthia
FaridCynthia
1Each Engineering employee matches exactly one department row — clean 1:1.
2If departments had TWO Engineering rows, each employee would appear twice.
3Row counts after a JOIN are worth a sanity check, always.
--key facts
Aliasese and d shorten the query — prefix every column for clarity
JOIN = INNER JOINthe bare keyword defaults to inner
Fan-out warningduplicate keys multiply rows — know your data
--use cases
Every normalized schema: ids in one table, names in another
Reports that need context: order + customer, employee + department
Data enrichment during analysis: facts + dimension tables
The #1 interview topic: explain INNER vs LEFT with an example
--under the hood
Nested-loop joins suit small outer rows with an index on the inner side; hash joins shine for big unsorted inputs.
Index the joined keys — the ON lookup happens per row, and a missing index turns microseconds into minutes.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.