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
2
u/Medohh2120 Oct 15 '25
Hi, a day from our great discussion on the "generalized left join" i had really enjoyed untill now, I am really learning alot form you!
after reading the link you gave me over and over again thouroghly i have came to a conclusion and correct me if i am wrong to ensure i got what the poster meant:
if i am not mistaken the only case a "generalized left join" is triggered is when there is no id joining or when condition is true where smth like t_a.id = 2 or 1=1 is "alone standing" but for 1=0 it works as a "classic left join" such as:
SELECT t_a.id, t_b.id
FROM t_a LEFT JOIN t_b ON t_a.id = 2
Here we get multiple results as expected (as 2 is called as many times as no. of rows as of table2)
but as soon as we add an equality condition to it results are actually making sense:
SELECT t_a.id, t_b.id
FROM t_a LEFT JOIN t_b ON t_a.id=t_b.id AND t_a.id = 2
as said in the post:
"Now this output seemingly matches the explanations from the beginning of this post. “All rows from the first table, with NULL values from non-matching rows”, or something like that."
corect me if i am wrong but yea as mentioned above: "generalized left join" is triggered only when there'sn't id joining
Let me throw some observations that may or may not be related as well:
Let's consider this example: https://ibb.co/cKYxqysn
details in next reply!!:
________________________________________