INNER JOIN

Sql tutorial · PySpark.in

7.2 INNER JOIN

Until now, we have seen how to combine tables using NATURAL JOIN.
Another very common and more widely used join is INNER JOIN.

An INNER JOIN returns only those rows where the specified condition is true in both tables.

In simple words, only matching rows from both tables are returned.

Syntax:

```

hide

sql

SELECT *

FROM table1

INNER JOIN table2

ON table1.c1 = table2.c2;

```

Note:
The ON clause specifies the condition used to match rows.
Although = is most common, other comparison operators can also be used depending on the requirement.

Example

Suppose we want to get the reviews of the course "Cyber Security" (where course_id = 15).

Here:

So, we use INNER JOIN to combine matching rows.

```

sql

-- Create student table

CREATE TABLE student (

student_id INT PRIMARY KEY,

full_name VARCHAR(100)

);

-- Create review table

CREATE TABLE review (

review_id INT PRIMARY KEY,

student_id INT,

course_id INT,

content VARCHAR(255),

created_at DATE

);

-- Insert students

INSERT INTO student (student_id, full_name) VALUES

(1, 'Amit Sharma'),

(2, 'Neha Verma'),

(3, 'Rohit Kumar'),

(4, 'Priya Singh');

-- Insert reviews

INSERT INTO review (review_id, student_id, course_id, content, created_at) VALUES

(101, 1, 15, 'Very practical and useful course.', '2026-05-01'),

(102, 2, 15, 'Good explanation of security concepts.', '2026-05-03'),

(103, 3, 12, 'Helpful for beginners.', '2026-05-04'),

(104, 4, 15, 'Excellent real-world examples.', '2026-05-05');

-- Get reviews of course "Cyber Security" (course_id = 15)

SELECT student.full_name, review.content, review.created_at

FROM student

INNER JOIN review

ON student.student_id = review.student_id

WHERE review.course_id = 15;

```

Important Point

An INNER JOIN returns only matching rows.

If a student exists in the student table but has no review in the review table, that student will not appear in the result.

Likewise, if a review contains a student_id that does not exist in the student table, that review will also not appear.

More Sql tutorials

All tutorials · Try the free PySpark compiler · Practice challenges