SQL LEFT JOIN
LEFT JOIN returns all rows from the left table, and matched rows from the right. If thereโs no match, right-side columns return NULL.
๐น Basic Syntax
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;๐น Example: List All Employees with Their Departments
SELECT
e.employee_name,
d.department_name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.department_id;โ
Employees with no department still appear, but department_name will be NULL.
๐น Use Case: Identify Unlinked Records
SELECT e.name
FROM employees e
LEFT JOIN departments d
ON e.department_id = d.id
WHERE d.id IS NULL;๐ก This finds employees not assigned to any department.
๐ง Quick Recap
| Key Point | Explanation |
|---|---|
LEFT JOIN | Keeps all records from left table |
| No match? | Right table columns return NULL |
| Helpful for | Finding missing or optional related data |
| Use with | WHERE to filter unmatched records (IS NULL) |
๐งฉ Use LEFT JOIN when the relationship is optional
