TutorialsSQLINNER JOIN
🟢 Free Demo
SQL TutorialTopic 11 of 20

INNER JOIN

Return only rows that match in both tables

✅ What You Will Learn

How INNER JOIN works and when to use it
How to write the ON clause to link two tables
How to use table aliases to keep JOIN queries readable
How to filter joined results with WHERE
How to JOIN more than two tables

INNER JOIN returns only the rows where the join condition matches in BOTH tables. If a row exists in the left table but has no match in the right table (or vice versa), it is excluded from the result.

This is the most commonly used JOIN in data analytics. Use it when you only want records that have complete information across both tables.

📋 customers table joined with orders table on customer_id

customer_idcustomer_namecityorder_idproductamount
1Rahul SharmaDelhi1001Laptop45000
1Rahul SharmaDelhi1003Headphones3500
2Priya VermaNoida1002Mobile Phone18000
4Sneha KapoorDelhi1004Laptop52000
5Vikram SinghNoida1005Tablet28000

Syntax

SQL SYNTAX
SELECT t1.column, t2.column
FROM table1 t1
INNER JOIN table2 t2
  ON t1.key_column = t2.key_column;

Examples

Example 1Join customers with their orders
SELECT c.customer_name,
       c.city,
       o.product,
       o.amount
FROM customers c
INNER JOIN orders o
  ON c.customer_id = o.customer_id;
OUTPUT
customer_name | city  | product      | amount
--------------|-------|--------------|-------
Rahul Sharma  | Noida | Laptop       | 45000
Rahul Sharma  | Noida | Headphones   | 3500
Priya Verma   | Delhi | Mobile Phone | 18000
💡

Amit Kumar (customer_id 3) has no orders — he is excluded. Order 1004 (customer_id 5) has no matching customer — it is also excluded. Only matched rows from both sides appear.

Example 2Aggregate after JOIN
SELECT c.customer_name,
       COUNT(o.order_id) AS total_orders,
       SUM(o.amount) AS total_spent
FROM customers c
INNER JOIN orders o
  ON c.customer_id = o.customer_id
GROUP BY c.customer_name
ORDER BY total_spent DESC;
OUTPUT
customer_name | total_orders | total_spent
--------------|--------------|------------
Rahul Sharma  | 2            | 48500
Priya Verma   | 1            | 18000
💡

JOIN first, then GROUP BY. This pattern — join tables, then aggregate — is used in almost every real analytics query.

Example 3Filter with WHERE after JOIN
SELECT c.customer_name, o.product, o.amount
FROM customers c
INNER JOIN orders o
  ON c.customer_id = o.customer_id
WHERE c.city = 'Noida'
  AND o.amount > 5000;

📌 Key Points to Remember

  • INNER JOIN only returns rows with a match in both tables
  • Rows without a match in either table are excluded
  • Always use table aliases (c, o) to keep queries readable
  • Use table.column notation (c.customer_name) when the same column name exists in both tables
  • JOIN first, filter with WHERE after, then GROUP BY

🏢 Real-World Application

INNER JOIN is the most common join in analytics. "Show each order with the customer name, city, and the product category" — that is an INNER JOIN between orders, customers, and products. At any data-driven company, a data analyst runs INNER JOINs to enrich transactional data with dimension data. Analysts at banks use INNER JOIN to link transaction records with customer KYC details. Analysts at logistics companies JOIN shipment records with warehouse location data. Mastering INNER JOIN unlocks the ability to answer cross-table business questions that are impossible with a single table.

⚠️ Common Mistakes to Avoid

WRONGForgetting the ON clause and getting a Cartesian product
FIXAlways include ON table1.key = table2.key. Without ON, SQL joins every row of table1 with every row of table2, creating an enormous result set that can crash your system.
WRONGNot using table aliases when joining, leading to ambiguous column names
FIXWhen two tables have a column with the same name (like customer_id), prefix with table name or alias: c.customer_id, o.customer_id. Without prefixes you'll get an 'ambiguous column' error.
WRONGUsing INNER JOIN when you need all rows from one table
FIXINNER JOIN drops rows with no match. If you want all customers even those with no orders, use LEFT JOIN with customers as the left table.
✏️Test Yourself

INNER JOIN returns rows where there is a match in...

❓ Frequently Asked Questions

What is INNER JOIN in SQL?

INNER JOIN returns rows where there is a matching value in both tables. Rows from either table that do not have a match in the other table are excluded from the result. It is the most commonly used join type.

What is the difference between JOIN and INNER JOIN?

They are identical. Writing JOIN without a type keyword defaults to INNER JOIN in all major databases. Best practice is to write INNER JOIN explicitly for clarity.

Can INNER JOIN return duplicate rows?

Yes. If the right table has multiple rows matching one row in the left table, the left row will appear multiple times in the result — once per match. This is correct behavior, not a bug.

How do I join three tables in SQL?

Chain multiple JOIN clauses: FROM orders o INNER JOIN customers c ON o.customer_id = c.customer_id INNER JOIN products p ON o.product_id = p.product_id. Each JOIN adds another table to the result.

What happens to non-matching rows in INNER JOIN?

They are discarded. If an order has a customer_id that does not exist in the customers table, that order row will not appear in the INNER JOIN result. Use LEFT JOIN to keep such rows.

✏️ Practice Exercise

Write a query to show customer name, city, and total amount spent — for customers from Noida only.

← PreviousSQL Joins — IntroductionNext →LEFT JOIN
🎓 Level Up Faster

Learn SQL with Live Trainer Guidance

These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.