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


 

 

Students: John, Raj, Emma, Sara
Departments: IT, HR, Finance

 

Students: John, Raj, Emma, Sara
Departments: IT, HR, Finance

 

Students: John, Raj, Emma, Sara
Departments: IT, HR, Finance

📌 Only rows where students.dept_id = departments.dept_id are included.


 Key Points

  1. Returns only matching rows.

  2. Excludes rows without a match in either table.

  3. Can join multiple tables using multiple INNER JOIN clauses.

  4. Often used in relational databases for combining related data.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *