r/SQL • u/Medohh2120 • 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!
View all comments
Show parent comment
1
u/squadette23 Oct 14 '25
> That's all good and makes sense but why is the output in the "1=0" example not the same as first?
Re-read the "Generalized LEFT JOIN algorithm" from https://minimalmodeling.substack.com/i/139115769/making-sense-of-generalized-left-join-behavior
> table1 R1 = table2 r5? ======> False
> then moving to 2nd row of table1:
Here, because for each row of table2 the match function returned false, we will emit the (table1.R1, NULL) output row.
LEFT JOIN is not very intuitive and not very natural. (It's not hard either, you just have to remember how it works).
It makes more intuitive sense for a subset of scenarios: matching by ID, and the table on the right has zero or one matching row.
For example:
select * from transactions t
left join users u
on t.user_id = u.id;
Here we show a list of transactions that show the information about user, if that user exists, or NULLs otherwise.