MySQL LEFT JOIN

MySQL LEFT JOIN

The LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table.
If there is no match, the result will still include the row from the left table, but the right table columns will show NULL.


 Syntax


  • table1 → Left table

  • table2 → Right table

  • ON → Join condition


🧠 Example Tables

Students Table (students)

student_id name dept_id
1 John 1
2 Emma 2
3 Raj 1
4 Sara 3
5 Aman 5

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
Aman NULL

✅ Notice: Aman is included even though dept_id = 5 has no match. The dept_name shows NULL.


 Venn Diagram Representation


 

LEFT JOIN result: All rows from students + matching rows from departments

  • Overlapping area → Matching dept_id rows

  • Left table exclusive → Rows included with NULL for right table


 Key Points

  1. Returns all rows from the left table.

  2. Returns matching rows from the right table, or NULL if no match.

  3. Useful when you want to include all records from the primary table even if related data is missing.

  4. Often used for reporting and finding unmatched records.

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 *