You can try this :
SELECT e.firstName, e.lastName, b.firstName, b.lastName
FROM employees as e
LEFT JOIN employees as b on b.employeeNumber = e.reportsTo;
- You select firtName and lastName of each employee
- You select firstName and lastName of their boss
- Using left join should return some NULL value for boss that report to no one
- I did the join on
b.employeeNumber = e.reportsTo
so you will get boss information from the employeeNumber where the employee report to (eg. employee 1056 report to employee 1002 so employee data are Patterson Mary and he reports to employee Murphy Diane, her boss)
EDIT :
After reading Hamza Haider answer about SELF JOIN
(name of what I did before with the join on same table) I find this other question where the answer looks like what you want and it will give you some information to understand how SELF JOIN
works : What is SELF JOIN and when would you use it?
According to the answer, here is an other solution :
SELECT
e1.FirstName,
e1.LastName,
e2.FirstName as SupervisorFirstName,
e2.LastName as SupervisorLastName
FROM employee e1
LEFT OUTER JOIN employee e2 on e2.employeeNumber = e1.reportsTo
The difference is the LEFT OUTER JOIN
and not LEFT JOIN
but they are both same (Are left outer joins and left joins the same?)