Lecture

INNER JOIN

An INNER JOIN returns rows where both tables contain matching values in the joined column.
It is the most commonly used join in SQL.


Syntax

Here is the syntax for an INNER JOIN:

INNER JOIN Syntax
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
  • ON defines how the two tables are linked.
  • The result only includes rows with matching keys in both tables.

Example: Students and Enrollments

Below is an example of a INNER JOIN between the students and enrollments tables.

students

student_idname
1John Smith
2Emily Davis
3Michael Lee
4Jessica Hall
5David Miller

enrollments

student_idclass_name
1Math
1History
2Biology
3Chemistry
6Geography
INNER JOIN example
SELECT students.name, enrollments.class_name FROM students INNER JOIN enrollments ON students.student_id = enrollments.student_id;

Result:

nameclass_name
John SmithMath
John SmithHistory
Emily DavisBiology
Michael LeeChemistry

Jessica Hall and David Miller are excluded since they have no enrollments. The enrollment for student_id = 6 is also excluded, as there is no matching student.


When should I use INNER JOIN?

Use an INNER JOIN when you need to retrieve related records that exist in both tables. Because it only returns rows found in both tables, it is especially useful for creating reports, checking participation, and analyzing linked data.

Quiz
0 / 1

What does an INNER JOIN do in SQL?

It returns all rows from both tables without any conditions.

It returns all rows from the left table and matched rows from the right table.

It returns only the rows with matching values in both tables.

It returns all rows from the right table and matched rows from the left table.

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Tables

Execution Result