PTA B1-7 Find the number of direct reports for each leader Score 10

SQL practice collection

Query the number of direct subordinates of each leader, display: the leader's employee number (EmployeeID), the number of subordinates (renamed to (countSub))

Tip: Please use the SELECT statement to answer.

Table Structure:

table sample

employees table:

Sample output:

Code length limit 16 KB

Time limit 400 ms

DatabaseMySQL

The result output requires a strict comparison of the order and data

select b.EmployeeID, count(*) countSub
from employees a, employees b
where a.ReportsTo = b.EmployeeID
group by b.EmployeeID 

Guess you like

Origin blog.csdn.net/weixin_70206677/article/details/129363048