Step 9
6 min read

INNER JOIN

Learn how to combine data from two tables - explained in the simplest way.

What is INNER JOIN?

Students Table
idname
1John
2Mary
3Peter
3 rows
Enrollments Table
enrollment_idstudent_idcourse
1011Math
1022Science
2 rows

INNER JOIN combines rows from two tables when they match.

Simple analogy: Matching students with their classes - show only students who are enrolled in classes.

Basic Example

Students Table
idname
1John
2Mary
3Peter
3 rows
Enrollments Table
enrollment_idstudent_idcourse
1011Math
1022Science
2 rows

Two tables:

Students:

  • ID 1: John
  • ID 2: Mary
  • ID 3: Peter (not enrolled)

Enrollments:

  • John enrolled in Math
  • Mary enrolled in Science
SELECT students.name, enrollments.course FROM students INNER JOIN enrollments ON students.id = enrollments.student_id;

Result: Only John and Mary (Peter not shown because he has no enrollment)

Real-Life Example

Students Table
idname
1John
2Mary
3Peter
3 rows
Enrollments Table
enrollment_idstudent_idcourse
1011Math
1022Science
2 rows

Customers and Orders:

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

Shows only customers who have orders.

When to Use

Students Table
idname
1John
2Mary
3Peter
3 rows
Enrollments Table
enrollment_idstudent_idcourse
1011Math
1022Science
2 rows

Use INNER JOIN when you want only matching records from both tables.

Example: Show students who are enrolled (exclude students with no classes)

What Comes Next

Students Table
idname
1John
2Mary
3Peter
3 rows
Enrollments Table
enrollment_idstudent_idcourse
1011Math
1022Science
2 rows

Next: LEFT JOIN (shows all records from left table, even without match)

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses