Guides › Self-join
Self-join in SQL Server: each person and their manager
The manager’s name lives in the same table. You join Staff to Staff and give each side an alias so SQL Server can tell them apart.
Staff with manager name
SELECT e.first_name AS staff,
e.team,
m.first_name AS manager
FROM Staff e
LEFT JOIN Staff m ON m.staff_id = e.manager_id
ORDER BY e.first_name;
LEFT JOIN so the person at the top (no manager) still appears. INNER JOIN would hide them.
Same alias twice
You cannot say FROM Staff JOIN Staff. Name them e and m.
Try it on Harbour & Co
Sandra Okafor (Managing Director) needs this now: “Org chart refresh: everyone and who they report to.” Write the query, run it, send the results. On the paid plan.
Take the ticket →Learn it on a real ticket
Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.
Clock in free See Monday’s tickets