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.
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 ASCby default but I wrote it nonetheless)
New Companies
You are viewing a single comment's thread. Return to all comments →
I did this by joining the least amount of tables for ease:
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
Explanation: The question asks us to identify the company code (
e.company_code
), the name of the founder (c.founder
) and the number ofDISTINCT
employees under each branch as the question illustrates that there is also the case sometimes there is only 1 employee under theLead_Manager
but sometimes there are 2 . That is whyDISTINCT
is used in conjuction to theCOUNT()
function.The
INNER JOIN
(or justJOIN
) is used using aliasing (eg.Company
is aliased asc
,Lead_Manager
is aliased aslm
,Senior_Manager
is aliased assm
,Manager
is aliased asm
andEmployee
ase
)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 theON
condition as followsON c.company_code = lm.company_code
) This will go on until i reachEmployees
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 thecompany_code
inASC
order (It isASC
by default but I wrote it nonetheless)There you go, hope this helped!