r/SQL • • Oct 13 '25

Discussion Still Confused by SQL Self-Join for Employee/Manager — How Do I “Read” the Join Direction Correctly?

I am still learning SQL, This problem has been with me for months:

SELECT e.employee_name, m.employee_name AS manager_name

FROM employees e

IINER JOIN employees m ON e.manager_id = m.employee_id;

I can't get my head around why reversing aliases yields different results since they are the same table like:

SELECT e.employee_name, m.employee_name AS manager_name

FROM employees e

IINER JOIN employees m ON m.manager_id = e.employee_id;

Could someone please explain it to me in baby steps?

edit: thanks for help everyone, I now get it if I draw it manually and use Left join matching algorithm, got both from commenters thanks!!, when I read how the rest thought my mind couldn't take it but I will be back!

20 Upvotes

28 comments sorted by

View all comments

Show parent comment

1

u/Medohh2120 Oct 14 '25

First thanks for the main topic explanation maybe the different result were because of the left join returning nulls after not finding matches, it's much clearer with inner join and you throwing your useful observation but here's a funny thing

if we wanted for reversing aliases to work we may break out brain cause now the only way to fix it is by renaming columns of e.employee_name AS Employee name, m.employee_name AS manager_name

to m.employee_name AS employee_name,e.employee_name as manager_name meaning the 2nd variant will be as follwing:

SELECT m.employee_name AS employee_name,e.employee_name as manager_name

FROM employees e

inner JOIN employees m ON m.manager_id = e.employee_id

which is more confusing, for now i'll try to stick with visual matching mentioned by a commenter above even tho i am sure there is a better way i can't get my head around