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 15 '25

________________________________________

SELECT a.id, b.a_id, b.status

FROM a

LEFT JOIN b ON a.id = b.a_id AND b.status = 'Active';

this returns expected results(matches and nulls for non-matches nothing repeatitive) despite having what the guy in the link called "generalized left join" for having another: condition b.status = 'Active'

_______________________________________

SELECT a.id, b.a_id, b.status

FROM a

LEFT JOIN b ON a.id = b.a_id where b.status = 'Active';

here we filtered via "Where" clause which gives out same results logically but not visually as it eliminates nulls for non-matches, effectively turning this cute left join into an inner one!:

SELECT a.id, b.a_id, b.status

FROM a

inner JOIN b ON a.id = b.a_id AND b.status = 'Active' ;