LEFT JOIN (or LEFT OUTER JOIN) in SQL is used to combine rows from two or more tables based on a related column between them. It returns all rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for columns from the right table. This type of join ensures that all records from the left table are included in the result set, regardless of whether they have a corresponding match in the right table.
Key Points
- All Rows from Left Table:
LEFT JOINreturns all rows from the left table, even if there are no matching rows in the right table. - Matching Rows from Right Table: If there is a match, the corresponding rows from the right table are included.
- NULL Values for Non-Matching Rows: If there is no match,
NULLvalues are returned for columns from the right table. - Alias Support: You can use table aliases to simplify queries.
Syntax
column1, column2, ...: The columns you want to retrieve.table1: The left table (all rows from this table are included).table2: The right table (only matching rows from this table are included).common_column: The column that relates the two tables.
Examples
Suppose you have two tables:Employees and Departments.
Table: Employees
| EmployeeID | Name | City | DepartmentID |
|---|---|---|---|
| 1 | Anand | Chennai | 101 |
| 2 | Bala | Coimbatore | 102 |
| 3 | Kavitha | Karaikal | 103 |
| 4 | Raj | Madurai | 104 |
| 5 | Kumar | Trichy | NULL |
| DepartmentID | DepartmentName |
|---|---|
| 101 | HR |
| 102 | Finance |
| 103 | IT |
| 104 | Marketing |
| Name | DepartmentName |
|---|---|
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | NULL |
Kumar does not have a department, so the DepartmentName is NULL.
Using LEFT JOIN with WHERE Clause
You can combineLEFT JOIN with the WHERE clause to filter the results further. For example, retrieve employees who do not belong to any department.
| Name | DepartmentName |
|---|---|
| Kumar | NULL |
Using LEFT JOIN with Multiple Tables
You can useLEFT JOIN to join more than two tables. Suppose you have an additional table named Projects.
Table: Projects
| ProjectID | ProjectName | DepartmentID |
|---|---|---|
| 201 | Project A | 101 |
| 202 | Project B | 102 |
| 203 | Project C | 103 |
| Name | DepartmentName | ProjectName |
|---|---|---|
| Anand | HR | Project A |
| Bala | Finance | Project B |
| Kavitha | IT | Project C |
| Raj | Marketing | NULL |
| Kumar | NULL | NULL |
Practical Use Case
Suppose you have a table namedStudents and another table named Courses.
Table: Students
| StudentID | Name | City | CourseID |
|---|---|---|---|
| 1 | Ram | Chennai | 101 |
| 2 | Karthik | Coimbatore | 102 |
| 3 | David | Bangalore | NULL |
| CourseID | CourseName |
|---|---|
| 101 | Mathematics |
| 102 | Science |
| 103 | English |
| Name | CourseName |
|---|---|
| Ram | Mathematics |
| Karthik | Science |
| David | NULL |
Key Takeaways
LEFT JOINreturns all rows from the left table and matching rows from the right table.- If there is no match,
NULLvalues are returned for columns from the right table. - It ensures that all records from the left table are included in the result set.
- You can use
LEFT JOINwith theWHEREclause to filter results further. LEFT JOINcan be used to join multiple tables in a single query.