MySQL INNER JOIN
MySQL INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
If a row in Table A has no corresponding row in Table B, it will not appear in the result.
Syntax
-
INNER JOIN→ Combines rows from both tables based on a related column. -
ON→ Specifies the condition for matching rows.
🧠 Example Tables
Students Table (students)
| student_id | name | dept_id |
|---|---|---|
| 1 | John | 1 |
| 2 | Emma | 2 |
| 3 | Raj | 1 |
| 4 | Sara | 3 |
Departments Table (departments)
| dept_id | dept_name |
|---|---|
| 1 | IT |
| 2 | HR |
| 3 | Finance |
| 4 | Sales |
SQL Query
Result
| name | dept_name |
|---|---|
| John | IT |
| Raj | IT |
| Emma | HR |
| Sara | Finance |
✅ Notice: Department “Sales” is not included because no student belongs to it.
Venn Diagram Representation
📌 Only rows where students.dept_id = departments.dept_id are included.
Key Points
-
Returns only matching rows.
-
Excludes rows without a match in either table.
-
Can join multiple tables using multiple
INNER JOINclauses. -
Often used in relational databases for combining related data.
