Checking your session…
Module 10: SQL Joins

10.2 INNER JOIN

Level: 10 | Version: v1.1 | Author: Meptrasoft

Overview

INNER JOIN returns only the rows that have a match in both tables.

For example:

TEXT
CUSTOMERS
    +
ORDERS
    ↓
INNER JOIN
    ↓
MATCHING RECORDS ONLY

INNER JOIN → Keep matching rows from both tables.

SQL INNER JOIN — Matching Rows
Figure 1: INNER JOIN returns only records that have matching values in both tables.

Practice Tables

This topic uses two tables:

Customers

customer_id customer_name city
1 Arun Kumar Chennai
2 Priya Sharma Mumbai
3 Ravi Patel Ahmedabad
4 Sneha Reddy Hyderabad
5 Vikram Singh Delhi
6 Meena Iyer Bangalore
7 Karthik Raj Chennai
8 Anjali Verma Pune
9 Rahul Das Kolkata
10 Divya Nair Kochi

Orders

order_id order_date amount customer_id
1 2026-01-01 1500 1
2 2026-01-05 2000 2
3 2026-01-07 500 1
4 2026-01-10 3000 3
5 2026-01-12 1200 4
6 2026-01-15 700 5
7 2026-01-18 2200 2
8 2026-01-20 900 6
9 2026-01-22 4000 3
10 2026-01-25 1100 7

Customers 8, 9, and 10 have no matching orders.


Basic INNER JOIN

SQL
SELECT customers.customer_name, orders.order_id
FROM customers
INNER JOIN orders
    ON customers.customer_id = orders.customer_id;

Result:

customer_name order_id
Arun Kumar 1
Priya Sharma 2
Arun Kumar 3
Ravi Patel 4
Sneha Reddy 5
Vikram Singh 6
Priya Sharma 7
Meena Iyer 8
Ravi Patel 9
Karthik Raj 10

Only customers with matching orders are returned.


How INNER JOIN Works

The database compares the join columns:

TEXT
customers.customer_id
          =
orders.customer_id

For example:

TEXT
Customer ID 1 → Order ID 1
Customer ID 1 → Order ID 3
Customer ID 2 → Order ID 2

A customer can therefore appear more than once when that customer has multiple orders.

One matching customer can produce multiple result rows.


Using Table Aliases

Aliases make join queries shorter and easier to read.

SQL
SELECT c.customer_name, o.order_id
FROM customers c
INNER JOIN orders o
    ON c.customer_id = o.customer_id;

Here:

TEXT
c → customers
o → orders

INNER JOIN vs No Match

Suppose customer_id = 8 has no order.

TEXT
Customers → 8
Orders    → no 8

With INNER JOIN, customer 8 is not returned.

TEXT
MATCH → INCLUDED
NO MATCH → EXCLUDED

This is the key difference to remember before learning LEFT JOIN.


Common Beginner Mistakes

✗ Mistake ✓ Correct Understanding
INNER JOIN returns every row from both tables It returns only matching rows
One customer always creates one result row Multiple orders can produce multiple rows
ON is optional for every INNER JOIN A normal join needs a condition unless using a different join form
JOIN combines tables permanently It creates a result set; it does not merge the physical tables
Customers without orders should appear INNER JOIN excludes unmatched customers

Placement Quick Points

TEXT
INNER JOIN
→ MATCHING ROWS ONLY

ON
→ JOIN CONDITION

MATCH
→ INCLUDED

NO MATCH
→ EXCLUDED
  • INNER JOIN combines matching rows from two tables.
  • The ON clause defines how the tables are matched.
  • Unmatched rows are excluded.
  • One row from one table can match multiple rows in the other table.
  • Aliases make join queries easier to read.

Interview Questions

What is an INNER JOIN?

An INNER JOIN returns rows where the join condition matches in both tables.

What happens to unmatched rows?

They are excluded from the result.

Can one customer appear multiple times?

Yes. If the customer has multiple matching orders, the customer can appear in multiple result rows.

What is the purpose of ON?

ON specifies the condition used to match rows between the tables.

Does INNER JOIN modify the original tables?

No. It produces a query result without changing the source tables.

What is the difference between JOIN and INNER JOIN?

JOIN is commonly used as shorthand for INNER JOIN.


Practice & Hands-On Exercises

Using customers and orders:

  1. Display customer names with their order IDs.
  2. Display customer names with order amounts.
  3. Find customers who have at least one order.
  4. Find customers who placed multiple orders.
  5. Explain why customers 8, 9, and 10 do not appear.
  6. Rewrite the query using table aliases:
SQL
SELECT c.customer_name, o.order_id, o.amount
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id;

💡 Tip: Test your queries using the in-browser interactive runner above.


Key Takeaway

TEXT
CUSTOMERS
    +
ORDERS
    ↓
INNER JOIN
    ↓
MATCHING RECORDS

INNER JOIN returns only the rows that match between the joined tables.

End of INNER JOIN