INNER JOIN
An INNER JOIN
returns rows where both tables have matching values in the joined column.
It's the most commonly used join in SQL.
Syntax
Here is the syntax for a INNER JOIN
:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
ON
defines how the two tables are linked.- Only rows with matching keys in both tables will be returned.
Example: Students and Enrollments
Below is an example of a INNER JOIN
between the students
and enrollments
tables.
students
student_id | name |
---|---|
1 | John Smith |
2 | Emily Davis |
3 | Michael Lee |
4 | Jessica Hall |
5 | David Miller |
enrollments
student_id | class_name |
---|---|
1 | Math |
1 | History |
2 | Biology |
3 | Chemistry |
6 | Geography |
SELECT students.name, enrollments.class_name FROM students INNER JOIN enrollments ON students.student_id = enrollments.student_id;
Result:
name | class_name |
---|---|
John Smith | Math |
John Smith | History |
Emily Davis | Biology |
Michael Lee | Chemistry |
Jessica Hall and David Miller are excluded because they have no enrollments. The enrollment for
student_id = 6
is also excluded because there is no matching student.
Why It Matters
Use INNER JOIN
when you:
- Need to connect related rows between tables
- Want only the rows where both sides match
- Build reports that rely on cross-referenced data
What's Next?
Coming up: LEFT JOIN
, which also includes unmatched rows from the left table.
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
Tables
Execution Result