SQL Inner Join (original) (raw)

Last Updated : 8 Sep, 2025

INNER JOIN is used to combine rows from two or more tables based on a related column. It returns only the rows that have matching values in both tables, filtering out non-matching records. It is commonly used in relational databases and useful for working with related data.

inner_join

Inner Join

**Syntax:

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

**Key Terms

Let's understand Inner Join better with examples.

Example of SQL INNER JOIN

To understand how INNER JOIN works, let’s first create two tables:

Both tables are linked through a common column, ID in the professor table and prof_id in the teacher table.

**professor Table

CREATE TABLE professor (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Salary INT );

INSERT INTO professor (ID, Name, Salary) VALUES
(1, Rohan Kumar, 57000),
(2, Hiroshi Tanaka, 45000),
(3, Maria Fernandez, 60000),
(4, Ahmed Hassan, 50000),
(5, Elena Petrova, 55000);

SELECT * FROM professor;

**Output

ID Name Salary
1 Rohan Kumar 57000
2 Hiroshi Tanaka 45000
3 Maria Fernandez 60000
4 Ahmed Hassan 50000
5 Elena Petrova 55000

**teacher Table

CREATE TABLE teacher (
course_id INT,
prof_id INT,
course_name VARCHAR(50) );

INSERT INTO teacher (course_id, prof_id, course_name) VALUES
(1, 1, 'English'),
(1, 3, 'Physics'),
(2, 4, 'Chemistry'),
(2, 5, 'Mathematics');

SELECT * FROM teacher;

**Output

course_id prof_id course_name
1 1 English
1 3 Physics
2 4 Chemistry
2 5 Mathematics

Now, if we want to join both tables to get combined information, we can use an INNER JOIN.

**For example, let’s retrieve course_id, prof_id, along with professor’s Name and Salary. The join condition is that the ID column from the professor table must match the prof_id column from the teacher table.

**Query:

SELECT teacher.course_id, teacher.prof_id, professor.Name, professor.Salary
FROM professor
INNER JOIN teacher ON professor.ID = teacher.prof_id;

**Output

course_id prof_id Name Salary
1 1 Rohan Kumar 57000
1 3 Maria Fernandez 60000
2 4 Ahmed Hassan 50000
2 5 Elena Petrova 55000

**Explanation: shows only professors who are assigned to a course. INNER JOIN matches rows where professor.ID equals teacher.prof_id, so only those professors appear. Professors without a course (like Hiroshi Tanaka) are excluded from the result.