Lecture

LEFT JOIN

A LEFT JOIN returns all rows from the left table along with any matching rows from the right table.

If there is no match, the result shows 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 (table1) is listed first.
  • 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 included even though they have no enrollments.


How is LEFT JOIN useful?

Use LEFT JOIN when you want to:

  • Keep all rows from the left table, regardless of matches
  • Identify records in the left table without corresponding entries in the right table
  • Build full lists, such as all customers with or without purchases
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