1270. All People Report to the Given Manager

https://leetcode.com/problems/all-people-report-to-the-given-manager

Description

Table: Employees


+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| employee\_id   | int     |
| employee\_name | varchar |
| manager\_id    | int     |
+---------------+---------+
employee\_id is the primary key for this table.
Each row of this table indicates that the employee with ID employee\_id and name employee\_name reports his work to his/her direct manager with manager\_id
The head of the company is the employee with employee\_id = 1.

Write an SQL query to find employee_id of all employees that directly or indirectly report their work to the head of the company.

The indirect relation between managers will not exceed 3 managers as the company is small.

Return result table in any order without duplicates.

The query result format is in the following example:


Employees table:
+-------------+---------------+------------+
| employee\_id | employee\_name | manager\_id |
+-------------+---------------+------------+
| 1           | Boss          | 1          |
| 3           | Alice         | 3          |
| 2           | Bob           | 1          |
| 4           | Daniel        | 2          |
| 7           | Luis          | 4          |
| 8           | Jhon          | 3          |
| 9           | Angela        | 8          |
| 77          | Robert        | 1          |
+-------------+---------------+------------+

Result table:
+-------------+
| employee\_id |
+-------------+
| 2           |
| 77          |
| 4           |
| 7           |
+-------------+

The head of the company is the employee with employee\_id 1.
The employees with employee\_id 2 and 77 report their work directly to the head of the company.
The employee with employee\_id 4 report his work indirectly to the head of the company 4 --> 2 --> 1. 
The employee with employee\_id 7 report his work indirectly to the head of the company 7 --> 4 --> 2 --> 1.
The employees with employee\_id 3, 8 and 9 don't report their work to head of company directly or indirectly. 

ac

Last updated