Course contentsShow
← Computer Science
Lesson 1577 of 2,87236. Database FundamentalsFree lesson

Inner Join Basics

Learn the INNER JOIN syntax and how it returns only matching rows from both tables.

Inner Join Basics

What you'll learn: You'll learn how to use INNER JOIN to combine rows from two tables based on a matching condition, returning only the data that exists in both tables.

What is an INNER JOIN?

An INNER JOIN is the most common type of join in SQL. It combines rows from two tables only when there's a match in both tables based on a specified condition (usually matching key values).

Think of it like matching puzzle pieces: you only get a complete picture when both pieces fit together. If a row in one table doesn't have a corresponding match in the other table, it simply won't appear in your results.

The Basic Syntax

SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

The ON clause specifies the join condition—typically matching a foreign key in one table to a primary key in another.

Simple Example

Imagine you have two tables: customers and orders.

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

This query returns only customers who have placed orders. If a customer hasn't ordered anything, they won't appear in the results. Similarly, if an order somehow references a non-existent customer (violating integrity), it won't appear either.

Why "Inner"?

The term "inner" emphasizes that only the intersection of the two tables is returned—the overlapping data where matches exist. Rows that don't match are excluded from both sides.

Key Takeaway: INNER JOIN returns only rows where the join condition finds matches in both tables, filtering out any unmatched data from either side.