skip to today's puzzle

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

Starting baristas data before the example query
idnamementor_id
1RowanNULL
2Maya1
3Juni2

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

baristamentor
JuniMaya
MayaRowan

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