Browse SQL Topics โ–ผ

JOINs

๐Ÿ“‚ Combining Tables (JOINs) ๐Ÿ“˜ Lesson 43 of 81

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:

  1. Store customer details once in a customers table, identified by a unique Primary Key (id).
  2. Store purchase details in an orders table, 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 (101 and 102).
  • Bob (id: 2) has one order (103).
  • Carlos (id: 3) has placed zero orders.
  • Order 104 has customer_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 104 is excluded because customer 99 does 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

  1. Forgetting the ON clause (The Cartesian Product Disaster): If you omit ON, SQL performs a CROSS 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.
  2. Ambiguous Column Name Error: If both customers and orders have a column named id, writing SELECT id causes an ambiguous column name: id error. Always qualify shared column names with table aliases: c.id or o.id.
  3. Filtering LEFT JOIN columns in WHERE: If you write LEFT JOIN orders o ON ... WHERE o.amount > 50, Carlos is discarded because NULL > 50 is not true. This accidentally converts your LEFT JOIN into an INNER JOIN! Move the filter condition into the ON clause 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.

Test your understanding of JOINs

Quick interactive practice questions with instant feedback.

Take Quiz โ†’