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 πΉ Example: List All Employees with Their Departments β Employees with no department still appear, but department_name will be NULL. πΉ Use Case: Identify Unlinked Records π‘ This finds […]
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
Was this helpful?
Thanks for your feedback!