β¬ οΈ The LEFT JOIN (also called LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table. If there is no matching row in the right table, SQL returns NULL for the right-table columns.
π What is a LEFT JOIN?
A LEFT JOIN keeps every record from the left table, regardless of whether a matching record exists in the right table. It is useful when you want complete information from one table while including related data from another table whenever available.
Information
π― Why Use LEFT JOIN?
Use a LEFT JOIN when every record from the left table is important, even if related data is missing.
- π Include all records from the primary table.
- π Identify records with no matching data.
- π Generate complete reports.
- π Work with optional relationships between tables.
π Sample Tables
Consider the following Students table:
| StudentID | Name | Department |
|---|---|---|
| 101 | Alice | Computer Science |
| 102 | Bob | Mathematics |
| 103 | Charlie | Physics |
| 104 | David | Computer Science |
Consider the following Courses table:
| CourseID | StudentID | CourseName |
|---|---|---|
| 1 | 101 | Database Systems |
| 2 | 101 | Operating Systems |
| 3 | 103 | Physics Lab |
| 4 | 104 | Computer Networks |
| 5 | 105 | Artificial Intelligence |
π Basic Syntax
LEFT JOIN Syntax
SELECT table1.column_name,
table2.column_name
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;π‘ Basic LEFT JOIN Example
Retrieve every student along with their enrolled course, if one exists.
Basic LEFT JOIN
SELECT Students.Name,
Courses.CourseName
FROM Students
LEFT JOIN Courses
ON Students.StudentID = Courses.StudentID;Result:
| Name | CourseName |
|---|---|
| Alice | Database Systems |
| Alice | Operating Systems |
| Bob | NULL |
| Charlie | Physics Lab |
| David | Computer Networks |
Important
π·οΈ Using Table Aliases
Aliases make join queries easier to read.
LEFT JOIN with Aliases
SELECT s.Name,
c.CourseName
FROM Students AS s
LEFT JOIN Courses AS c
ON s.StudentID = c.StudentID;π LEFT JOIN with WHERE
You can filter the joined results using the WHERE clause.
Filter LEFT JOIN Results
SELECT s.Name,
c.CourseName
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
WHERE s.Department = 'Computer Science';π« Finding Unmatched Rows
One of the most common uses of LEFT JOIN is finding records in the left table that have no matching rows in the right table.
Students Without Courses
SELECT s.Name
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
WHERE c.StudentID IS NULL;This query returns students who are not enrolled in any course.
π LEFT JOIN with ORDER BY
Sort the joined results using the ORDER BY clause.
Sort LEFT JOIN Results
SELECT s.Name,
c.CourseName
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY s.Name ASC;π LEFT JOIN Multiple Tables
You can join multiple related tables in a single query.
LEFT JOIN Three Tables
SELECT s.Name,
c.CourseName,
d.DepartmentName
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
LEFT JOIN Departments d
ON s.DepartmentID = d.DepartmentID;π How LEFT JOIN Works
| Students Table | Courses Table | Included? |
|---|---|---|
| Matching StudentID | Matching StudentID | β Yes |
| No Matching Course | No Match | β Yes (Course columns are NULL) |
| No Matching Student | Course Exists | β No |
βοΈ INNER JOIN vs LEFT JOIN
| Feature | INNER JOIN | LEFT JOIN |
|---|---|---|
| Matching Rows | β Yes | β Yes |
| Unmatched Left Rows | β Excluded | β Included |
| Unmatched Right Rows | β Excluded | β Excluded |
β οΈ Common Mistakes
- β Using the wrong join column.
- β Forgetting the ON clause.
- β Filtering NULL values incorrectly.
- β Confusing LEFT JOIN with INNER JOIN.
Warning
πΌ Real-World Example
A university administrator wants a report listing every student and the course they are enrolled in. Students without any course enrollment should still appear in the report.
Student Enrollment Report
SELECT s.StudentID,
s.Name,
s.Department,
c.CourseName
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID
ORDER BY s.Name;This query ensures that every student is included, even if no course has been assigned.
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π LEFT JOIN returns all rows from the left table.
- π Matching rows from the right table are included.
- π Unmatched right-table columns contain NULL.
- π It is useful for finding missing relationships between tables.
- π Table aliases improve readability.
- π IS NULL helps identify unmatched rows after a LEFT JOIN.