SELF JOIN
A self-join is a table joined to itself with two aliases. employees e JOIN employees boss ON e.manager_id = boss.id; each person plus their manager’s name. Same table, two nicknames.
Need a hierarchy or ‘pair rows in the same table’. Students sitting in the same city: s1 JOIN s2 ON s1.city = s2.city AND s1.roll < s2.roll to avoid pairing Asha with Asha.
Trap: self-join without aliases — MySQL cannot tell which column is which. Trap: forgetting the extra AND to avoid duplicate pairs.
On the example next to this theory — SELF JOIN — jOIN combines rows using a match condition between tables.
students.id
=
marks.student_id
│
▼
one result rowemployees e JOIN employees boss ON e.manager_id = boss.id