I am having trouble wrapping my head around the on statement when doing a self-join. Let's say we have the following table:
| employeeid | name | managerid | salary | 
|---|---|---|---|
| 1 | Mike | 3 | 35000 | 
| 2 | Rob | 1 | 45000 | 
| 3 | Todd | NULL | 25000 | 
| 4 | Ben | 1 | 55000 | 
| 5 | Sam | 1 | 65000 | 
I want to perform a self join to return the employee name and their manager's name.
When I perform the following self join I get an incorrect result:
SELECT E.name as Employee,M.name as Manager
FROM tblEmployees E
LEFT JOIN tblEmployees M
ON E.Employeeid=M.managerid
However, when I reverse the columns on the on statement using the query below:
SELECT E.name as Employee,M.name as Manager
FROM tblEmployees E
LEFT JOIN tblEmployees M
ON E.managerid=M.Employeeid
I get the correct answer.
Why? How do I know which columns to select in an on statement?
 
     
    