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 = 5has no match. Thedept_nameshows NULL.
Venn Diagram Representation
-
✅ Overlapping area → Matching dept_id rows
-
✅ Left table exclusive → Rows included with NULL for right table
Key Points
-
Returns all rows from the left table.
-
Returns matching rows from the right table, or NULL if no match.
-
Useful when you want to include all records from the primary table even if related data is missing.
-
Often used for reporting and finding unmatched records.
