LEFT JOIN
LEFT JOIN returns all records from the left table and the matching records from the right table.
If there is no matching record in the right table, the columns from the right table contain NULL values.
What is LEFT JOIN?
A LEFT JOIN returns every row from the left table, regardless of whether a matching row exists in the right table.
When a matching record is found, information from both tables is returned. When no match is found, the columns from the right table contain NULL.
LEFT JOIN Syntax
SELECT columns
FROM Table1
LEFT JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;
Example Tables
Students Table — Left Table
| StudentId | StudentName | CourseId |
|---|---|---|
| 101 | Rahul | 1 |
| 102 | Priya | 2 |
| 103 | Amit | 1 |
| 104 | Neha | 3 |
| 105 | Rohan | 5 |
Courses Table — Right Table
| CourseId | CourseName |
|---|---|
| 1 | .NET Full Stack |
| 2 | Java Full Stack |
| 3 | Python Full Stack |
| 4 | Software Testing |
Basic LEFT JOIN Example
Here, Students is the left table and Courses is the right table.
SELECT
Students.StudentName,
Courses.CourseName
FROM Students
LEFT JOIN Courses
ON Students.CourseId = Courses.CourseId;
Result
Because Students is the left table, all students are returned, including students whose CourseId does not exist in the Courses table.
| StudentName | CourseName |
|---|---|
| Rahul | .NET Full Stack |
| Priya | Java Full Stack |
| Amit | .NET Full Stack |
| Neha | Python Full Stack |
| Rohan | NULL |
LEFT JOIN with WHERE
LEFT JOIN can be combined with a WHERE condition to filter the result.
SELECT
S.StudentName,
C.CourseName
FROM Students AS S
LEFT JOIN Courses AS C
ON S.CourseId = C.CourseId
WHERE S.StudentName LIKE 'R%';
This returns students whose names start with R while preserving the LEFT JOIN behavior.
Finding Records Without a Match
LEFT JOIN can be used with IS NULL to find records in the left table that do not have a matching record in the right table.
SELECT
S.StudentName,
S.CourseId
FROM Students AS S
LEFT JOIN Courses AS C
ON S.CourseId = C.CourseId
WHERE C.CourseId IS NULL;
This returns students whose CourseId has no matching course in the Courses table.
LEFT JOIN using Aliases
Aliases make LEFT JOIN queries shorter and easier to understand.
SELECT
S.StudentName,
C.CourseName
FROM Students AS S
LEFT JOIN Courses AS C
ON S.CourseId = C.CourseId;
Selecting Multiple Columns
SELECT
S.StudentId,
S.StudentName,
S.CourseId,
C.CourseName
FROM Students AS S
LEFT JOIN Courses AS C
ON S.CourseId = C.CourseId;
LEFT JOIN vs INNER JOIN
| LEFT JOIN | INNER JOIN |
|---|---|
| Returns all records from the left table. | Returns only matching records. |
| Non-matching right-side values become NULL. | Non-matching records are excluded. |
| Useful when all left-table records are required. | Useful when only matching records are required. |
Important Points
- LEFT JOIN returns every record from the left table.
- Matching records from the right table are included.
- If there is no match, right-table columns contain NULL.
- The ON clause defines the relationship between the tables.
- LEFT JOIN can be used to find unmatched records using IS NULL.
Summary
LEFT JOIN returns all records from the left table and matching records from the right table. When a matching record is not available in the right table, NULL values are returned for the right-table columns.