To
retrieve all employee names along with their corresponding manager names and
IDs from a table with columns id, name, and managerID, you can
use a self-join in SQL. Here's the query:
SELECT
e.id AS
EmployeeID,
e.name AS
EmployeeName,
m.id AS
ManagerID,
m.name AS
ManagerName
FROM
Employee
e
LEFT JOIN
Employee
m ON e.managerID = m.id;
Explanation:
- Employee e: refers to the employee.
- Employee m: refers to the manager
(same table).
- LEFT JOIN: ensures all employees are included,
even if they don't have a manager (e.g., top-level executives).
- e.managerID = m.id: matches each employee’s
manager ID to the manager’s actual ID.
Let me
know if you want to filter by specific manager, sort by hierarchy, or export
this as a guide.
No comments:
Post a Comment