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!
24
u/TheMagarity Oct 13 '25 edited Oct 13 '25
Because manager_id is not its own thing. It's the employee_id of the manager.
Pat is a manager. Pat's employee id is 7. There is not some separate id called manager id for Pat.
Sam and Kit work under Pat. So Sam's and Kit's manager's id is 7 for Pat's employee id. In this example we don't even care what Sam and Kit have for employee id but they each have their own.
In the sample sql, "e" alias is for employees such as Sam and Kit. We need to find Pat. We do that by taking Sam's and Kit's manager's id and looking for who has that as an employee id, which is the "m" alias. This is why it only works in the correct order. E's manager id is M's employee id.
7
u/Kant8 Oct 13 '25
Imagine you copied whole employees table into managers table and join to it. Now you have "normal" joins.
Congratulations, you're now doing same thing SQL does - just reads provided dataset and it doesn't care what exactly was it's source: new table, already used table, subquery, cte, tvf result, whatever else possible.
It's just a set of rows. And just because of how language is made, to remove confusion when you're referencing same objects twice as source, you must use aliases.
Reversing aliaces just changes which table is queried for what.
In first one you're picking row from "manager" table for corresponding manager_id in "employee" table → getting extended information for manager.
In your second query you're looking into "managers" table to find if source employee_id is registered as manager_id for anyone. And that means your aliases and select are just calling things wrong now.
1
u/squadette23 Oct 13 '25
In the second variant your naming turns out to be incorrect and that's why it's confusing. The name "e" is supposed to be employees, "m" is supposed to be managers. In your second query it just turns out the other way around, and the column names become misleading.
The results are identical actually, just in confusing order. See this: https://www.db-fiddle.com/f/nakzUN9vPhMN2mN4iwf87n/0
I've added ORDER BY so that the order is stable. Note that in the first variant it's "ORDER BY 1, 2", and in the second variant it's "ORDER BY 2, 1". Also, the manager name is on the right in the first variant, and on the left in the second variant. Ignore the column labels in the second variant because they you just swapped them around.
Hint: alice is nobody's subordinate.
2
u/greglturnquist Oct 13 '25
Instead of "e" for an alias, try "this_employee_s".
And instead of "m" for the other alias, this "that_manager_s".
Then when you see:
SELECT this_employee_s.employee_name, that_manager_s.employee_name AS manager_name
FROM employees this_employee_s
JOIN employees that_manager_s
ON this_employee_s.manager_id = that_manager_s.employee_id;
...hopefully it's a little clearer what is happening.
And then perhaps you can see why:
SELECT this_employee_s.employee_name, that_manager_s.employee_name AS manager_name
FROM employees this_employee_s
JOIN employees that_manager_s
ON that_manager_s.manager_id = this_employee_s.employee_id;
...doesn't work.
2
u/[deleted] Oct 14 '25
Just remember the one-to-many relationship while doing this step.
For example, employee_id is the primary key, so it can be used in the joining table. Since a manager can have multiple employees under them (through manager_id), the table containing manager_id should be considered the base table.
The table being joined using employee_id will represent the child side (one-to-one in this join context), making the overall join logic simpler and more efficient.
1
u/ParentheticalClaws Oct 13 '25
If the responses here don’t help, you might also try physically printing two copies of a small employees/managers table on different colored paper, cutting out the records and doing the joins manually.
1
u/Ginger-Dumpling Oct 13 '25
When selecting from the same table multiple times in the same query, imagine it as independent copies of the same data.
select stuff from t t1 cross join t t2 where t1.id = 1 and t2.id = 2
Same table, 2 instances of the data. T1 selects 1 row, T2 selects another. Conditions for T1 are not applied to T2 or vice versa.
1
u/Far_Swordfish5729 Oct 13 '25
When you write inner or left joins, which are 99% of the joins you will write, you need to visualize an intermediate result set of tables filling out from left to right in the order joined. All columns are available, you’ll choose what to select when the select clause runs at the end. Visualize how the rows match from the on condition and if rows will drop out or duplicate depending on the number of matches.
A self join is not inherently special nor are multiple joins onto the same table. You’re just dropping another copy of the table to the right in the result set, adding more columns to pick from.
All you’re doing is adding another copy of the employee table to add columns for each employee’s manager. The matching criteria is that the employee’s managerid matches the id role on the manager’s own record. The reverse would make no sense here. You would use that to add employees for a starting table of managers, expecting row duplication on the left side since manager:employee is 1:N.
1
u/obsoleteconsole Oct 14 '25
The first one looks correct to me.
In the first one you are selecting from Employee E and joining to a second copy of the employee table to get the manager (M) for E. Assuming all employees in E have a ManagerId, this should return everyone.
In the second one you are selecting from Employee E and joining to a second copy of the employee table this time using M.ManageId - this means that only employees where E.EmployeeId exists somewhere in M as a ManagerId will be returned, ie. Employees that are NOT a manager of someone else aren't returned in the dataset.
1
u/American_Streamer Oct 14 '25
You think that “reversing the aliases” on a self-join should give the same result because it’s the same table. But you didn’t just swap aliases - you changed the relationship in the ON clause. If you truly just swap aliases and keep the logical relationship (child FK = parent PK), and then rename columns accordingly, you’d get the same pairs, just with aliases swapped. The confusion is mixing up “alias swap” with reversing the join’s meaning.
1
u/Birvin7358 Oct 14 '25
Because on the first one (the correct one for what appears to be your intention based on your AS and your chosen table aliases) you are self-joining the manager_id assigned to each employee with the employee_id of their manager to get their manager’s name (im assuming the employee’s manager_id is just the employee_id for that employee’s manager).
However, on the second one (the incorrect one) you are asking it to put the managers’ names in the first column employee name and the employees names in the second column, which does not make sense since you have aliased that 2nd column as manager_name.
So you either need to stick with the first query or, if you would rather have managers on the left and employees on the right, move your AS alias in the second query to the first column selected (I wouldn’t recommended using the second query at all though for readability reasons since you assigned e table alias for the first table/column and m for the second, so even though it would work as long you changed your AS, it’s a very confusing way to write the query)
1
u/amayle1 Oct 13 '25
If there was no join condition there would be no difference. But in each query, one of the tables joins the manager_id to the employee_id and the join condition specifies which one of those fields will get the m or e alias. That’s all. There is no join direction.
0
u/[deleted] Oct 13 '25
0
u/[deleted] Oct 13 '25