JOINs
Combine columns from multiple related tables using INNER JOIN, LEFT JOIN, and table aliases linked by primary and foreign keys.
Why are relational databases called relational?
In a spreadsheet or flat CSV file, people often duplicate information on every single row: the customerโs name, email, street address, and phone number are repeated next to every single item they purchase.
If that customer changes their home address, you would have to locate and edit thousands of rows across your spreadsheet. If you miss even one row, your database becomes corrupted and inconsistent.
To solve this, database architects use a design process called normalization:
- Store customer details once in a
customerstable, identified by a unique Primary Key (id). - Store purchase details in an
orderstable, referencing the customer using a Foreign Key (customer_id).
In SQL, JOIN is the mechanism used to stitch these related tables back together on the fly when answering queries.
The Two Tables in Our Example
TABLE: customers (c)
โโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ id โ name โ city โ
โโโโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโโโค
โ 1 โ Alice Johnson โ London โ
โ 2 โ Bob Smith โ New York โ
โ 3 โ Carlos Ruiz โ Madrid โ
โโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
TABLE: orders (o)
โโโโโโโโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโ
โ order_id โ customer_id โ product โ amount โ
โโโโโโโโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโผโโโโโโโโโค
โ 101 โ 1 โ Mechanical KBโ 120.00 โ
โ 102 โ 1 โ USB Mouse โ 30.00 โ
โ 103 โ 2 โ Desk Lamp โ 45.00 โ
โ 104 โ 99 โ Headset โ 80.00 โ (orphan order)
โโโโโโโโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโดโโโโโโโโโ
Notice:
- Alice (
id: 1) has two orders (101and102). - Bob (
id: 2) has one order (103). - Carlos (
id: 3) has placed zero orders. - Order
104hascustomer_id: 99, which doesnโt exist in our customer table.
1. INNER JOIN: The Perfect Match
An INNER JOIN returns only the rows where there is a valid match in both tables:
SELECT c.name, c.city, o.product, o.amount
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;
Result:
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโ
โ name โ city โ product โ amount โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโค
โ Alice Johnson โ London โ Mechanical KB โ 120.00 โ
โ Alice Johnson โ London โ USB Mouse โ 30.00 โ
โ Bob Smith โ New York โ Desk Lamp โ 45.00 โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโ
Notice what is missing:
- Carlos (
id: 3) is excluded because he has no orders. - Order
104is excluded because customer99does not exist.
2. LEFT JOIN: Keep Everything on the Left
What if your sales team asks: โGive me a list of every single customer, along with their orders if they have any?โ
If you use INNER JOIN, Carlos disappears completely. To keep all records from the first (left) table regardless of whether they have matches, use LEFT JOIN (also known as LEFT OUTER JOIN):
SELECT c.name, c.city, o.product, o.amount
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;
Result:
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโ
โ name โ city โ product โ amount โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโค
โ Alice Johnson โ London โ Mechanical KB โ 120.00 โ
โ Alice Johnson โ London โ USB Mouse โ 30.00 โ
โ Bob Smith โ New York โ Desk Lamp โ 45.00 โ
โ Carlos Ruiz โ Madrid โ NULL โ NULL โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโ
Because Carlos has no matching orders, the database fills his product and amount columns with NULL.
The Anti-Join Pattern: Finding Inactive Users
You can use LEFT JOIN combined with WHERE ... IS NULL to find records that have no relationship:
-- Find all customers who have NEVER placed an order:
SELECT c.id, c.name, c.city
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE o.order_id IS NULL;
Result:
โโโโโโฌโโโโโโโโโโโโโโฌโโโโโโโโโ
โ id โ name โ city โ
โโโโโโผโโโโโโโโโโโโโโผโโโโโโโโโค
โ 3 โ Carlos Ruiz โ Madrid โ
โโโโโโดโโโโโโโโโโโโโโดโโโโโโโโโ
Table Aliases: Keeping Queries Clean
Instead of writing out long table names over and over:
-- VERBOSE AND CLUTTERED:
SELECT customers.name, orders.amount
FROM customers
JOIN orders ON orders.customer_id = customers.id;
-- CLEAN AND IDIOMATIC:
SELECT c.name, o.amount
FROM customers c
JOIN orders o ON o.customer_id = c.id;
Assign short 1-to-2 letter aliases (c for customers, o for orders) directly after the table name in the FROM and JOIN lines.
Joining Three or More Tables
Real applications often chain multiple joins together:
SELECT
c.name AS customer_name,
o.order_id,
p.name AS product_name,
p.price
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id
INNER JOIN order_items oi ON oi.order_id = o.order_id
INNER JOIN products p ON p.id = oi.product_id
WHERE c.city = 'London';
The database resolves each join sequentially, connecting customer to order, order to line item, and line item to product details.
Common Traps to Avoid
- Forgetting the
ONclause (The Cartesian Product Disaster): If you omitON, SQL performs aCROSS JOIN. It pairs every row in Table A with every row in Table B. If Table A has 5,000 customers and Table B has 10,000 orders, you will accidentally generate 50,000,000 rows, causing massive memory spikes and server slowdowns. - Ambiguous Column Name Error: If both
customersandordershave a column namedid, writingSELECT idcauses anambiguous column name: iderror. Always qualify shared column names with table aliases:c.idoro.id. - Filtering
LEFT JOINcolumns inWHERE: If you writeLEFT JOIN orders o ON ... WHERE o.amount > 50, Carlos is discarded becauseNULL > 50is not true. This accidentally converts yourLEFT JOINinto anINNER JOIN! Move the filter condition into theONclause if you want to keep unmatched left rows.
Next Steps
Master this concept to build a rock-solid foundation. Next, proceed to left-outer-join.