MySQL LEFT JOIN

MySQL Tutorial

MySQL LEFT JOIN

MySQL 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_idnamedept_id
1John1
2Emma2
3Raj1
4Sara3
5Aman5

Departments Table (departments)

dept_iddept_name
1IT
2HR
3Finance
4Sales

 SQL Query


 Result

namedept_name
JohnIT
RajIT
EmmaHR
SaraFinance
AmanNULL

 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.

You may also like...