ADVERTISEMENT

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 PointExplanation
LEFT JOINKeeps all records from left table
No match?Right table columns return NULL
Helpful forFinding missing or optional related data
Use withWHERE to filter unmatched records (IS NULL)

🧩 Use LEFT JOIN when the relationship is optional

ADVERTISEMENT