Skip to main content

Inner Join in MySQL

In this tutorial, we will learn about the Inner Join in MySQL. Inner Join is a type of JOIN which retrieves records from multiple tables where the given condition is satisfied. Let's dive in.

What is Inner Join in MySQL?

Inner Join in MySQL is used to return rows from both tables that satisfy the given condition. It compares each row of the first table with each row of the second table to find all the pairs of rows that satisfy the join predicate.

For example, if you have two tables named Students and Courses, and you want to find all the students who are taking a particular course, you can use an Inner Join to achieve this.

Syntax of Inner Join

The basic syntax of Inner Join in MySQL is as follows:

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

Here, columns are the names of the columns you want to select. table1 and table2 are the names of the tables you want to join, and table1.column = table2.column is the condition for the join.

How to Use Inner Join in MySQL

Let's consider the following two tables:

Table1: Students

IDName
1Alice
2Bob
3Carol

Table2: Courses

IDCourse
1Math
2English
3Science

Now, let's say we want to find the names of the students along with the courses they are taking. Here's how we can do it using Inner Join:

SELECT Students.Name, Courses.Course
FROM Students
INNER JOIN Courses
ON Students.ID = Courses.ID;

This would give us the following result:

NameCourse
AliceMath
BobEnglish
CarolScience

Here, the Inner Join combines each row from the Students table with each row from the Courses table where the ID matches.

Conclusion

Inner Join is a powerful tool in MySQL that allows you to retrieve data from multiple tables based on a related column between them. However, remember that it only returns the rows where there is a match based on the given condition.

Practice using Inner Join with different tables and conditions to get a better understanding of how it works. Happy learning!