A self-join connects a table to itself
A self-join reads the same table in two roles. Here one baristas alias is the learner and the other is the mentor. ON matches the learner's mentor_id to the mentor's id, just as a normal JOIN matches related rows. The aliases keep those roles clear; they do not create new stored tables. This INNER JOIN shows only baristas with a matching mentor, so Rowan's own row is not in the result.
Starting table: baristas
| id | name | mentor_id |
|---|---|---|
| 1 | Rowan | NULL |
| 2 | Maya | 1 |
| 3 | Juni | 2 |
A tiny example
In plain English: Show me each barista who has a mentor, next to that mentor’s name.
SELECT b.name AS barista, m.name AS mentor
FROM baristas b
JOIN baristas m
ON b.mentor_id = m.id
ORDER BY b.name;What comes back
| barista | mentor |
|---|---|
| Juni | Maya |
| Maya | Rowan |
Juni's mentor_id points to Maya (2), and Maya's points to Rowan (1). Rowan has no mentor, so this INNER JOIN leaves that row out.
Related SQL lessons
Ready for a break?
Take on today's cozy puzzle. A fresh, bite-sized query waits every morning — no timers, no pressure.
play today's sip