LEFT JOIN in SQL

⬅️ 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

The left table is the table written before the LEFT JOIN keyword, while the right table is the table written after it.

🎯 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:

StudentIDNameDepartment
101AliceComputer Science
102BobMathematics
103CharliePhysics
104DavidComputer Science

Consider the following Courses table:

CourseIDStudentIDCourseName
1101Database Systems
2101Operating Systems
3103Physics Lab
4104Computer Networks
5105Artificial 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:

NameCourseName
AliceDatabase Systems
AliceOperating Systems
BobNULL
CharliePhysics Lab
DavidComputer Networks

Important

Student Bob appears even though no matching course exists. However, the course with StudentID = 105 is not included because it exists only in the right table.

🏷️ 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 TableCourses TableIncluded?
Matching StudentIDMatching StudentIDβœ… Yes
No Matching CourseNo Matchβœ… Yes (Course columns are NULL)
No Matching StudentCourse Exists❌ No

βš–οΈ INNER JOIN vs LEFT JOIN

FeatureINNER JOINLEFT 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

When searching for unmatched rows, use IS NULL instead of = NULL, because NULL cannot be compared using the equality operator.

πŸ’Ό 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

Use LEFT JOIN when all records from the left table must be included. Join using related keys, qualify column names with table aliases, use IS NULL to find unmatched records, and retrieve only the columns you need instead of using SELECT * in production queries.

πŸš€ 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.
>>"A LEFT JOIN never forgets the left tableβ€”it includes every row, whether a match exists or not."

Summary

βœ… The LEFT JOIN is an essential SQL join that returns every row from the left table while adding matching data from the right table when available. It is widely used for generating complete reports, identifying missing relationships, and working with optional data in relational databases.