Stage 4. JOIN: Combining Relational Data Correctly

JOIN connects rows from multiple tables using a relationship condition. In relational systems this is a core skill: data is split by entities (users, orders, payments), and without JOIN you cannot build complete business views.

Why JOIN is needed

Example task: show order list with customer name. Name is in users, order is in orders. Reading those tables separately does not produce one row with “order + owner”. JOIN solves that.

Base pattern:

SELECT ...
FROM left_table l
JOIN right_table r ON r.key = l.key;

Critical part: the ON condition. It defines which rows match.

JOIN types from simple to advanced

1) INNER JOIN

Returns only rows that match on both sides.

INNER JOIN schema

SELECT u.name, o.id AS order_id, o.amount
FROM users u
INNER JOIN orders o ON o.user_id = u.id;

Users without orders are excluded.

2) LEFT JOIN

Returns all rows from left table plus matched rows from right table. If no match exists, right-side fields are NULL.

LEFT JOIN schema

SELECT u.name, o.id AS order_id, o.amount
FROM users u
LEFT JOIN orders o ON o.user_id = u.id;

A user without orders remains as name + NULL.

3) RIGHT JOIN

Mirror of LEFT JOIN: all rows from right table plus matches from left.

RIGHT JOIN schema

SELECT u.name, o.id AS order_id
FROM users u
RIGHT JOIN orders o ON o.user_id = u.id;

In practice, teams often prefer LEFT JOIN style for readability.

4) FULL OUTER JOIN

Returns matches plus unmatched rows from both sides.

FULL JOIN schema

SELECT u.name, o.id AS order_id
FROM users u
FULL OUTER JOIN orders o ON o.user_id = u.id;

Useful for reconciliation and orphan-record checks.

Example with output (INNER JOIN)

SELECT o.id, o.amount, o.status, u.name
FROM orders o
INNER JOIN users u ON u.id = o.user_id
ORDER BY o.id;
id amount status name
1 1200.00 PAID Anna
2 300.00 NEW Anna
3 700.00 PAID Ivan

Common mistakes and fixes

  1. Missing ON condition: creates Cartesian explosion.
  2. Wrong post-filter after LEFT JOIN: WHERE o.status = 'PAID' may remove left-side unmatched rows.
  3. Ignoring cardinality: one user can have many orders, so row count growth is expected.

Safer pattern for right-side filter in LEFT JOIN:

LEFT JOIN orders o
  ON o.user_id = u.id
 AND o.status = 'PAID'

How to learn JOIN without confusion

  1. Start with two-table INNER JOIN.
  2. Rewrite same case with LEFT JOIN and compare output.
  3. Add third table (payments) and observe row-count growth.
  4. Then move to FULL OUTER JOIN and reconciliation cases.
Glossary
table
A database structure that stores data in rows and columns.
row
One record inside a database table.
column
A named field in a database table that stores one kind of value.
schema
The structure that defines database objects such as tables, columns, relationships, and constraints.
primary key
A column or group of columns that uniquely identifies each row in a table.
foreign key
A column or group of columns that points to a primary key in another table.
relationship
A connection between data entities, usually represented through keys or object references.
entity
A domain object or persistent class that represents something with identity.
attribute
A property of an entity, usually mapped to a column or field.
aggregate function
A SQL function that calculates one value from many rows, such as count, sum, or average.
index
A data structure that helps a database find rows faster without scanning the whole table.
selectivity
A measure of how well a column or condition narrows data to a smaller result set.
normalization
The process of organizing relational data to reduce duplication and protect consistency.
transaction
A group of operations treated as one unit that should complete fully or not apply at all.
ACID
The database transaction guarantees of atomicity, consistency, isolation, and durability.
isolation level
A database setting that controls how concurrent transactions see each other's changes.
lock
A database or concurrency mechanism that restricts access to shared data while work is in progress.
deadlock
A situation where operations wait on each other in a cycle and cannot continue without intervention.
query plan
The database engine's chosen strategy for executing a query.
EXPLAIN
A SQL command that shows the query plan selected by the database.
sequential scan
A query operation that reads table rows one by one instead of using a targeted index lookup.