We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Advanced Select
- New Companies
- Discussions
New Companies
New Companies
Sort by
recency
|
3032 Discussions
|
Please Login in order to post a comment
SELECT C.company_code, C.founder, COUNT(DISTINCT LM.lead_manager_code) AS total_lead_managers, COUNT(DISTINCT SM.senior_manager_code) AS total_senior_managers, COUNT(DISTINCT M.manager_code) AS total_managers, COUNT(DISTINCT E.employee_code) AS total_employees FROM Company C LEFT JOIN Lead_Manager LM ON C.company_code = LM.company_code LEFT JOIN Senior_Manager SM ON LM.lead_manager_code = SM.lead_manager_code LEFT JOIN Manager M ON SM.senior_manager_code = M.senior_manager_code LEFT JOIN Employee E ON M.manager_code = E.manager_code GROUP BY C.company_code, C.founder ORDER BY C.company_code;
You do not have to use any tables except the primary company table and the employees table, because the employees table contains all the data from all the managerial tables:
select c.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 as c, employee as e where e.company_code = c.company_code group by c.company_code, c.founder order by c.company_code;
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 as c join lead_manager as l using (company_code) join senior_manager as s using (lead_manager_code) join manager as m using (senior_manager_code) join employee as e using (manager_code) GROUP BY C.company_code , C.FOUNDER ORDER BY C.company_code;
SELECT C.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 , Lead_Manager LM , Senior_Manager SM , Manager M ,Employee E
WHERE C.company_code = LM.company_code AND LM.company_code = SM.company_code AND SM.company_code = M.company_code AND M.company_code = E.company_code
GROUP BY C.company_code , C.FOUNDER ORDER BY C.company_code;