• + 3 comments

    I did this by joining the least amount of tables for ease:

    SELECT e.company_code, c.founder,
    COUNT(DISTINCT e.lead_manager_code),
    COUNT(DISTINCT e.senior_manager_code),
    COUNT(DISTINCT e.manager_code),
    COUNT(DISTINCT e.employee_code)
    FROM Company c
    INNER JOIN Employee e
    ON c.company_code = e.company_code
    GROUP BY e.company_code, c.founder
    ORDER BY e.company_code ASC
    

    But if you are keen on joining multiple tables to get practice, you can use the following code. It is built on INNER JOINing multiple tables

    SELECT e.company_code, c.founder,
    COUNT(DISTINCT lm.lead_manager_code),
    COUNT(DISTINCT sm.senior_manager_code),
    COUNT(DISTINCT m.manager_code),
    COUNT(DISTINCT e.employee_code)
    FROM Company c
    INNER JOIN Lead_Manager lm
    ON c.company_code = lm.company_code
    INNER JOIN Senior_Manager sm
    ON lm.lead_manager_code = sm.lead_manager_code
    INNER JOIN Manager m
    ON sm.senior_manager_code = m.senior_manager_code
    INNER JOIN Employee e
    ON m.manager_code = e.manager_code
    GROUP BY e.company_code, c.founder
    ORDER BY e.company_code ASC
    

    Explanation: The question asks us to identify the company code (e.company_code), the name of the founder (c.founder) and the number of DISTINCT employees under each branch as the question illustrates that there is also the case sometimes there is only 1 employee under the Lead_Manager but sometimes there are 2 . That is why DISTINCT is used in conjuction to the COUNT() function.

    The INNER JOIN (or just JOIN) is used using aliasing (eg. Company is aliased as c, Lead_Manager is aliased as lm, Senior_Manager is aliased as sm, Manager is aliased as m and Employee as e)

    For ease of understanding, I used the same formats I have joined the tables (eg. if FROM Company c INNER JOIN Lead_Manager lm, then I also used the same format for the ON condition as follows ON c.company_code = lm.company_code) This will go on until i reach Employees Table.

    Since we need to use GROUP BY, I used the non-aggregated values (i.e. SELECT e.company_code, c.founder) and ordered them by the company_code in ASC order (It is ASC by default but I wrote it nonetheless)

    There you go, hope this helped!