Lecture

LEFT JOIN

A LEFT JOIN returns all rows from the left table, and the matching rows from the right table.

If there is no match, the result will contain NULL for columns from the right table.


Syntax

Here is the syntax for a LEFT JOIN:

LEFT JOIN Syntax
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
  • The left table is listed first (table1).
  • All rows from the left table are included, even if there is no match in the right table.

Example: Students and Enrollments

Below is an example of a LEFT JOIN between the students and enrollments tables.

students

student_idname
1John Miller
2Lisa Brown
3David Smith
4Emily Davis
5Michael Jones

enrollments

student_idclass_name
1Math
1Science
2History
5Art
LEFT JOIN example
SELECT students.name, enrollments.class_name FROM students LEFT JOIN enrollments ON students.student_id = enrollments.student_id;

Result:

nameclass_name
John MillerMath
John MillerScience
Lisa BrownHistory
David SmithNULL
Emily DavisNULL
Michael JonesArt

David Smith and Emily Davis appear even though they have no enrollments.


Why It Matters

LEFT JOIN is useful when:

  • You want to include all entries from the left table
  • You need to identify unmatched records
  • You're generating full lists (e.g. all customers with or without purchases)

What's Next?

Next, you'll learn about the RIGHT JOIN, which works in the opposite direction by preserving all rows from the right table.

Quiz
0 / 1

In a LEFT JOIN operation, unmatched rows from the right table will be included with NULL values for their columns in the result.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Tables

Execution Result