# INNER JOIN

> Combine two tables into one result with INNER JOIN, and see which rows find a partner and which quietly disappear.

Subject: SQL | Lesson 13 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/inner-join
Previous: [CASE Expressions](https://codewithanimation.in/sql/case-expressions.md) | Next: [LEFT, RIGHT & FULL Joins](https://codewithanimation.in/sql/outer-joins.md)

## A join glues rows from two tables

Data lives in separate tables so nothing is stored twice. A **join** builds one wider table out of two by pairing up rows. You tell the database how to pair them with an `ON` test.

`INNER JOIN` keeps a pair only when the `ON` test is true. A row that finds no partner is left out. `INNER` is the default, so a plain `JOIN` means the same thing.

## Answers live across tables

A question like "which customer bought a Notebook?" needs the customer name from one table and the item from another. A join is how one query reads both.

## Match customers to their orders

```sql
SELECT c.name, o.item
FROM customers AS c
INNER JOIN orders AS o
  ON c.id = o.customer_id;
```

`c` and `o` are short names (aliases) for the two tables. The `ON` test says a customer matches an order when the customer's id equals the order's `customer_id`.

Asha has two orders, so she appears twice. Ben has one. Chen, Dara and Eli have no orders, so they find no partner and do not appear at all.

## Common mistake: A wrong ON is still valid SQL

If you compare the wrong columns, such as `c.id = o.id`, the query runs without any error and returns nothing, because customer ids 1 to 5 never equal order ids 101 to 103. Always check the row count against what you expect.

Rows that do not match vanish without a warning. If you needed every customer in the result, `INNER JOIN` is the wrong tool, and the next lesson shows the right one.

## Tip: Name the key on both sides

Write the join as the parent key equals the child key, `c.id = o.customer_id`, and give each table a short alias. Then check the result with a small count before trusting it.

## Try it: Match and mismatch

Switch the ON test to the wrong key and watch the result empty out. Then add the Pune filter and see how WHERE trims the joined rows.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/inner-join

## Check yourself

### Question 1

`customers` has 5 rows and only Asha and Ben have orders (3 orders in all). How many rows does `customers INNER JOIN orders ON c.id = o.customer_id` return?

- A. 3
- B. 5
- C. 2

Correct answer: A

One result row is made for each matching pair. Asha has 2 orders and Ben has 1, so there are 3 rows.

- Why not B: That would keep customers without orders. INNER JOIN drops them.
- Why not C: Two customers match, but Asha has two orders, so she makes two rows.

### Question 2

What happens to Chen, who has no orders, in an INNER JOIN with orders?

- A. Chen is left out of the result, with no error
- B. Chen appears with NULL in the order columns
- C. The query fails because Chen has no match

Correct answer: A

INNER JOIN keeps only pairs that satisfy ON, so a row with no partner silently disappears.

- Why not B: That is what a LEFT JOIN does, not an INNER JOIN.
- Why not C: A row with no partner is normal and is simply skipped.

### Question 3

A support ticket has `customer_id` set to NULL. Does `c.id = t.customer_id` match it to any customer?

- A. No, because comparing with NULL is never true
- B. Yes, to the first customer
- C. Yes, to every customer

Correct answer: A

NULL = anything is unknown, not true. An INNER JOIN keeps only true matches, so the ticket is dropped.

- Why not B: A NULL key is never matched to anything.
- Why not C: That would need the test to be true, and a comparison with NULL is unknown.

## Remember this

- A join pairs rows from two tables using an ON test, and INNER JOIN keeps only the pairs where ON is true.
- A row with no partner is dropped without any error.
- A row with several partners appears once for each partner.
- A NULL key never matches, so rows with a NULL key do not survive an INNER JOIN.
