Skip to content

Commit 32750e1

Browse files
add 1270
1 parent 3a25e14 commit 32750e1

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ _If you like this project, please leave me a star._ ★
885885
|1308|[Running Total for Different Genders](https://leetcode.com/problems/running-total-for-different-genders/)|[Solution](../master/database/_1308.sql) || Medium |
886886
|1294|[Weather Type in Each Country](https://leetcode.com/problems/weather-type-in-each-country/)|[Solution](../master/database/_1294.sql) | | Easy |
887887
|1280|[Students and Examinations](https://leetcode.com/problems/students-and-examinations/)|[Solution](../master/database/_1280.sql) | [:tv:](https://www.youtube.com/watch?v=ThbkV4Fs7iE)| Easy |
888+
|1270|[All People Report to the Given Manager](https://leetcode.com/problems/all-people-report-to-the-given-manager/)|[Solution](../master/database/_1270.sql) || Medium |
888889
|1251|[Average Selling Price](https://leetcode.com/problems/average-selling-price/)|[Solution](../master/database/_1251.sql) | | Easy |
889890
|1241|[Number of Comments per Post](https://leetcode.com/problems/number-of-comments-per-post/)|[Solution](../master/database/_1241.sql) | | Easy |
890891
|1179|[Reformat Department Table](https://leetcode.com/problems/reformat-department-table/)|[Solution](../master/database/_1179.sql) | | Easy |

database/_1270.sql

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--1270. All People Report to the Given Manager
2+
--
3+
--Table: Employees
4+
--
5+
--+---------------+---------+
6+
--| Column Name | Type |
7+
--+---------------+---------+
8+
--| employee_id | int |
9+
--| employee_name | varchar |
10+
--| manager_id | int |
11+
--+---------------+---------+
12+
--employee_id is the primary key for this table.
13+
--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
14+
--The head of the company is the employee with employee_id = 1.
15+
--
16+
--
17+
--Write an SQL query to find employee_id of all employees that directly or indirectly report their work to the head of the company.
18+
--
19+
--The indirect relation between managers will not exceed 3 managers as the company is small.
20+
--
21+
--Return result table in any order without duplicates.
22+
--
23+
--The query result format is in the following example:
24+
--
25+
--Employees table:
26+
--+-------------+---------------+------------+
27+
--| employee_id | employee_name | manager_id |
28+
--+-------------+---------------+------------+
29+
--| 1 | Boss | 1 |
30+
--| 3 | Alice | 3 |
31+
--| 2 | Bob | 1 |
32+
--| 4 | Daniel | 2 |
33+
--| 7 | Luis | 4 |
34+
--| 8 | Jhon | 3 |
35+
--| 9 | Angela | 8 |
36+
--| 77 | Robert | 1 |
37+
--+-------------+---------------+------------+
38+
--
39+
--Result table:
40+
--+-------------+
41+
--| employee_id |
42+
--+-------------+
43+
--| 2 |
44+
--| 77 |
45+
--| 4 |
46+
--| 7 |
47+
--+-------------+
48+
--
49+
--The head of the company is the employee with employee_id 1.
50+
--The employees with employee_id 2 and 77 report their work directly to the head of the company.
51+
--The employee with employee_id 4 report his work indirectly to the head of the company 4 --> 2 --> 1.
52+
--The employee with employee_id 7 report his work indirectly to the head of the company 7 --> 4 --> 2 --> 1.
53+
--The employees with employee_id 3, 8 and 9 don't report their work to head of company directly or indirectly.
54+
55+
--# Write your MySQL query statement below
56+
select e3.employee_id from Employees e1, Employees e2, Employees e3
57+
where e1.manager_id = 1 and e2.manager_id = e1.employee_id and e3.manager_id = e2.employee_id and e3.employee_id != 1

0 commit comments

Comments
 (0)