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!

19 Upvotes

28 comments sorted by

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!!:

________________________________________

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' ;

1

u/squadette23 Oct 15 '25

> corect me if i am wrong but yea as mentioned above: "generalized left join" is triggered only when there'sn't id joining

yes.

> what the guy in the link

I am the guy in the link btw.

Your example with statuses is great, but I think that I disagree that

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

> this returns expected results

I mean, of course yes, this is exactly how LEFT JOIN actually works, so you can say that this is "expected result". BUT: I believe that there are some scenarios where this is actually not what you expect (because you did not look closely enough or you're tired).

If I glance at the query and I see "b.status = 'Active'" I would probably expect to only see active results! (And note that with INNER JOIN this is exactly how it works!)

Also, here it's clearly visible because we have small clear queries and we can discuss them. But what if this is a subquery in some huge query of hundreds of lines. You may not be so lucky to notice that one of them has this gotcha.

Back in the day I wrote this example of generalized LEFT JOIN gotcha: https://docs.google.com/document/d/1d22QtBRFthalVn3Y8UhtYQ4IDf3qkmcLHNL23JE-r7s/edit?usp=sharing (as google docs because it contains some tables for reference). Commenting is enabled.

I'm also going to write another example, somewhat based on your example.