01 / 20

SQL
INNER JOIN

Learn how to combine rows from two or more tables by matching values in a shared column: the essential building block of relational queries.

SQL

Why Do We Need to Join Tables?

Relational databases spread data across multiple tables to avoid duplication. A school database might store students in one table and enrollments in another. To answer "Which courses is Ada enrolled in?" you must connect rows that share the same student ID.

Key Terms to Know

Primary Key
A column (or set of columns) that uniquely identifies each row in a table. Example: student_id.
Foreign Key
A column in one table that references the primary key of another table, creating a link between them.
Join Condition
The ON clause that specifies which columns must match for rows to be combined.

Visualizing INNER JOIN

Table A only Table B only INNER JOIN Only rows with matches in BOTH tables are returned

The Syntax of INNER JOIN

SELECT columns FROM table_a INNER JOIN table_b ON table_a.key = table_b.key;

How Row Matching Works

students id name 1 Ada 2 Bo 3 Cal enrollments student_id course 1 Math 1 Art 3 Bio no match id = student_id Result: 3 rows Ada Math ...

Ada (id=1) matches two enrollment rows, so she appears twice. Bo (id=2) has no enrollment, so he is excluded entirely.

INNER vs. LEFT vs. CROSS JOIN

INNER JOIN LEFT JOIN CROSS JOIN
Returns Only matched rows All left rows + matches Every combination
Unmatched left rows Excluded Kept (NULLs) N/A
ON clause Required Required None
Use when You need only matches You need all left rows Generating combos

Step-by-Step Approach

1
Identify the tables you need. Which table has the data you want to display?
2
Find the shared column: the foreign key in one table that references the primary key in the other.
3
Write FROM + INNER JOIN: place the first table after FROM, the second after INNER JOIN.
4
Set the ON clause: table_a.key = table_b.key.
5
SELECT columns from either or both tables. Prefix with the table name if column names overlap.

Example 1: Basic Join

Goal: List every student alongside the course they are enrolled in.

SELECT students.name, enrollments.course FROM students INNER JOIN enrollments ON students.id = enrollments.student_id;
namecourse
AdaMath
AdaArt
CalBio

Example 2: Using Aliases

Table names can get long. Aliases let you abbreviate them for readability.

SELECT s.name, e.course, e.grade FROM students s INNER JOIN enrollments e ON s.id = e.student_id WHERE e.grade >= 90;

Example 3: Joining Three Tables

Goal: Show each student's name, course name, and teacher name.

SELECT s.name AS student, c.title AS course, t.name AS teacher FROM students s INNER JOIN enrollments e ON s.id = e.student_id INNER JOIN courses c ON e.course_id = c.id INNER JOIN teachers t ON c.teacher_id = t.id;

Does Table Order Matter?

INNER JOIN
Order does not change the result set. A JOIN B returns the same rows as B JOIN A.
LEFT JOIN
Order matters. The "left" table keeps all its rows. Swapping tables changes which rows are preserved.

Explicit vs. Implicit Join Syntax

Explicit (recommended)
FROM students
INNER JOIN enrollments
  ON students.id = enrollments.student_id
Implicit (old style)
FROM students, enrollments
WHERE students.id = enrollments.student_id

Query Execution Order

1
FROM: the database identifies the source tables.
2
INNER JOIN + ON: rows are matched across tables using the join condition.
3
WHERE: the combined rows are filtered further.
4
SELECT: the chosen columns are extracted from the surviving rows.
5
ORDER BY: the result set is sorted last.

Common Mistakes

✕ Forgetting the ON clause
Without ON, some databases return a cross join: every row paired with every other row, producing a massive result.
✕ Wrong key column
Joining on a column that is not the actual foreign key produces garbage matches or an empty result.
✕ Ambiguous column names
If both tables have a column called name, you must prefix it: students.name vs. teachers.name.

INNER JOIN Cheat Sheet

Concept Key Detail
What it returns Only rows with a match in both tables
Required clause ON table_a.key = table_b.key
Table order Does not affect the result
Multiple joins Chain: INNER JOIN ... ON ... INNER JOIN ... ON
Duplicates One-to-many keys create multiple rows
Best practice Use explicit syntax + table aliases

Question 1 of 5

What does an INNER JOIN return?

Question 2 of 5

Given the tables orders (with column customer_id) and customers (with column id), which ON clause correctly joins them?

Question 3 of 5

Table A has 5 rows. Table B has 4 rows. Two rows in A have no match in B. How many rows does A INNER JOIN B return (assuming each matched A-row matches exactly one B-row)?

Question 4 of 5

What does this query return?

Question 5 of 5

What happens if you write INNER JOIN but forget the ON clause entirely?