(because sometimes you want everyone… even if they didn’t show up)
👀 Imagine You Have These Two Tables
🧑 employees
table:
emp_id | name |
---|---|
1 | Olivia |
2 | Mason |
3 | Zoe |
4 | Ethan |
📅 attendance
table:
record_id | emp_id | day |
---|---|---|
201 | 1 | 2024-04-01 |
202 | 2 | 2024-04-01 |
203 | 1 | 2024-04-02 |
You want to know who was present… but you ALSO want to see who was not.
Basically: “Show me all employees, even if they didn’t mark attendance.”
🔧 LEFT JOIN Syntax
SELECT employees.name, attendance.day
FROM employees
LEFT JOIN attendance
ON employees.emp_id = attendance.emp_id;
SQL will return all employees from the left table,
and match whatever it can from attendance
. If there’s no match? You get NULL
.
✅ Result
name | day |
---|---|
Olivia | 2024-04-01 |
Olivia | 2024-04-02 |
Mason | 2024-04-01 |
Zoe | NULL |
Ethan | NULL |
Zoe and Ethan didn’t check in — but they still show up in the result.
🔁 LEFT JOIN vs INNER JOIN
Type | Who’s Shown |
---|---|
INNER JOIN | Only employees with attendance |
LEFT JOIN | All employees, even without records |
🧃 Recap
LEFT JOIN
gives you all rows from the left, and matches from the right- If there’s no match, you get
NULL
- Use it when the left side is your focus (like “all users”, “all products”, etc.)