Problem
Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy:

Given the table schemas below, write a query to print the
company_code,
founder name, total number of
lead managers, total number of
senior managers, total number of
managers, and total number of
employees. Order your output by ascending
company_code.
Note:
- The tables may contain duplicate records.
- The company_code is string, so the sorting should not be numeric. For example, if the company_codes are C_1, C_2, and C_10, then the ascending company_codes will be C_1, C_10, and C_2.
Sample Output
C1 Monika 1 2 1 2
C2 Samantha 1 1 2 2
Explanation
In company C1, the only lead manager is LM1. There are two senior managers, SM1 and SM2, under LM1. There is one manager, M1, under senior manager SM1. There are two employees, E1 and E2, under manager M1.
In company C2, the only lead manager is LM2. There is one senior manager, SM3, under LM2. There are two managers, M2 and M3, under senior manager SM3. There is one employee, E3, under manager M2, and another employee, E4, under manager, M3.
My Answer
SET sql_mode='';
SELECT DISTINCT
C.company_code,
C.founder,
COUNT(DISTINCT L.lead_manager_code),
COUNT(DISTINCT S.senior_manager_code),
COUNT(DISTINCT M.manager_code),
COUNT(DISTINCT E.employee_code)
FROM Company AS C
LEFT JOIN Lead_Manager AS L
ON C.company_code = L.company_code
LEFT JOIN Senior_Manager AS S
ON L.lead_manager_code = S.lead_manager_code
LEFT JOIN Manager AS M
ON S.senior_manager_code = M.senior_manager_code
LEFT JOIN Employee AS E
ON M.manager_code = E.manager_code
GROUP BY C.company_code
ORDER BY C.company_code
Answer from others
set sql_mode='';
SELECT
c.company_code,
c.founder,
COUNT(DISTINCT l.lead_manager_code),
COUNT(DISTINCT s.senior_manager_code),
COUNT(DISTINCT m.manager_code),
COUNT(DISTINCT e.employee_code)
FROM Company c,
Lead_Manager l,
Senior_Manager s,
Manager m,
Employee e
WHERE c.company_code = l.company_code
AND l.lead_manager_code = s.lead_manager_code
AND s.senior_manager_code = m.senior_manager_code
AND m.manager_code = e.manager_code
GROUP BY c.company_code
ORDER BY c.company_code;
The below query cannot work right!
SELECT C.company_code,C.founder,NumL,NumS,NumM,NumE
FROM(select NumE,NumM,NumS,count(distinct L.lead_manager_code) as NumL,L.company_code
FROM(select NumE,NumM,count(distinct S.senior_manager_code) as NumS, S.lead_manager_code
FROM(SELECT NumE,COUNT(distinct M.manager_code) AS NumM,M.senior_manager_code
FROM (SELECT COUNT(DISTINCT employee_code) AS NumE,manager_code
FROM Employee
GROUP BY manager_code) AS E
RIGHT JOIN Manager AS M
ON M.manager_code = E.manager_code
GROUP BY M.senior_manager_code) AS EM
RIGHT JOIN Senior_Manager as S
on S.senior_manager_code = EM.senior_manager_code
GROUP BY S.lead_manager_code) as EMS
RIGHT JOIN Lead_Manager as L
on L.lead_manager_code = EMS.lead_manager_code
group by L.company_code) AS EMSL
RIGHT JOIN Company as C
on C.company_code = EMSL.company_code
order by company_code;