01 / 21

Table Joins
Connecting Your Data

Learn how INNER JOIN and LEFT JOIN combine rows from multiple tables, and how to interpret the results in real business scenarios.

Business Analytics

Why Do We Need Table Joins?

Real business databases store data across many tables rather than one giant spreadsheet. A company might have a Customers table, an Orders table, and a Products table: each focused on one subject.

To answer a question like "Which customers placed orders last month?" you need to combine rows from both the Customers and Orders tables. That combination is called a join.

The Shared Key Column

Customers customer_id name 101 Alice 102 Bob 103 Carol match on key Orders customer_id amount 101 $250 102 $180 101 $75 The shared column customer_id exists in both tables, enabling the join.

Visualizing Joins: The Venn Diagram

Table A only Table B only Matching Rows The overlap contains rows where the key values match.

Think of each table as a circle. The overlap region represents rows where the shared key values exist in both tables. Different join types select different regions of this diagram.

What Is an INNER JOIN?

INNER JOIN excluded excluded Only returns rows that match in BOTH tables.
SELECT * FROM Customers AS c INNER JOIN Orders AS o ON c.customer_id = o.customer_id;

What Is a LEFT JOIN?

matched kept (with NULLs) excluded LEFT table RIGHT table Returns ALL rows from the left table, plus matches from the right.
SELECT * FROM Customers AS c LEFT JOIN Orders AS o ON c.customer_id = o.customer_id;

INNER vs LEFT: The Core Difference

INNER JOIN
Returns only matching rows. Rows without a partner in the other table are excluded entirely. Result set can be smaller than either source table.
LEFT JOIN
Returns all rows from the left table. Non-matching rows appear with NULL values for the right table's columns. Result has at least as many rows as the left table.

The ON Clause: Setting the Match Rule

The ON clause specifies which columns must match between the two tables. It acts as the bridge connecting rows from left to right.

ON c.customer_id = o.customer_id
1
Prefix with table alias: Use c. and o. to avoid ambiguity when both tables share a column name.
2
Use a single equality: The most common pattern is table_a.key = table_b.key.
3
Multiple conditions: You can add AND to join on more than one column when a single column is not unique enough.

How to Write a Join Query

1
Identify the business question: What data do you need, and which tables hold it?
2
Find the shared key: Determine which column(s) link the tables together.
3
Choose the join type: Need all records from one side? Use LEFT JOIN. Only overlapping records? Use INNER JOIN.
4
Write FROM ... JOIN ... ON: Place the primary table after FROM, the secondary after JOIN, and the match condition after ON.
5
Select the columns you need: List specific columns in SELECT rather than using *.
6
Verify your results: Check row count and look for unexpected NULL values.

Example 1: INNER JOIN in Action

Customers
customer_idname
101Alice
102Bob
103Carol
→
Orders
customer_idamount
101$250
102$180
101$75
SELECT c.name, o.amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id;
Result: 3 rows
nameamount
Alice$250
Alice$75
Bob$180

Example 2: LEFT JOIN in Action

SELECT c.name, o.amount FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id;
Result: 4 rows
nameamount
Alice$250
Alice$75
Bob$180
CarolNULL

Example 3: Finding Inactive Customers

A marketing team wants to find customers who have never placed an order so they can send a promotional email. This is a classic LEFT JOIN + NULL filter pattern.

SELECT c.name, c.email FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id WHERE o.customer_id IS NULL;
Result: 1 row
nameemail
Carolcarol@email.com

Why Joins Can Multiply Rows

Alice (101) Order #1: $250 Order #2: $75 Result: 2 rows for Alice Alice | $250 Alice | $75 One-to-many relationship: 1 customer → many orders

Understanding NULL in Join Results

In a LEFT JOIN, NULL appears in the right table's columns when there is no matching row. NULL is not zero, not an empty string: it means "no data."

NULL in Aggregations
Functions like SUM and AVG skip NULL values. COUNT(column) ignores NULLs, but COUNT(*) counts all rows.
IS NULL Check
Never write = NULL. Always use IS NULL or IS NOT NULL to test for missing values.
COALESCE Fallback
Use COALESCE(column, 0) to replace NULL with a default value in your results.

Common Mistakes with Joins

✕ Forgetting the ON Clause
Without ON, every row in Table A is paired with every row in Table B: a cross join that can produce millions of rows.
✕ Wrong Join Type
Using INNER JOIN when you need all left-side rows silently drops records. Always ask: "Do I need unmatched rows?"
✕ Ignoring Duplicates
A one-to-many join multiplies rows. If you sum a dollar column without accounting for this, totals will be inflated.

Summary: Join Types at a Glance

Feature INNER JOIN LEFT JOIN
Rows returned Only matching rows All left + matching right
Unmatched left rows Dropped Kept (with NULLs)
Unmatched right rows Dropped Dropped
Result row count Can be fewer or more rows than either table ≥ left table
NULLs in result No (from join logic) Yes (right-side columns)
Best for Active relationships Complete inventories

Question 1 of 5

You have a Products table with 50 rows and an Orders table with 200 rows. After an INNER JOIN on product_id, the result has 180 rows. What does this tell you?

Question 2 of 5

Which join type should you use to find all employees, including those who have not been assigned to any project?

Question 3 of 5

In a LEFT JOIN result, a row shows NULL for the right table's order_date column. What does this mean?

Question 4 of 5

A Students table has 100 rows. A Grades table has 500 rows (each student can have multiple grades). After a LEFT JOIN, how many rows will the result have at minimum?

Question 5 of 5

What happens if you write a JOIN without an ON clause?