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
|
3294 Discussions
|
Please Login in order to post a comment
Guys this is the simplest query by using last table which is having entire data except founders. Hope you find it useful.
SELECT E.company_code, C.founder, E.total_LM, E.total_SM, E.total_MC, E.total_EC FROM ( SELECT company_code, COUNT(DISTINCT lead_manager_code) AS total_LM, COUNT(DISTINCT senior_manager_code) AS total_SM, COUNT(DISTINCT manager_code) AS total_MC, COUNT(DISTINCT employee_code) AS total_EC FROM Employee GROUP BY company_code ) AS E JOIN Company AS C ON E.company_code = C.company_code ORDER BY E.company_code;
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 c left join Employee e on c.company_code=e.company_code GROUP by c.company_code , c.founder order by c.founder this query is running but answer is wrong why that so
I know there are simpler ways to solve this problem, but I want to figure it out using a non-correlated subquery, because I've just studied it.
My solution in MySQL:
there is no need to use all the tables. All the components are present in the employee table except founders names. so we can just use the company table and the employee table here's my code: SELECT c.company_code, c.founder, COUNT(DISTINCT e.lead_manager_code ) AS total_lm, COUNT(DISTINCT e.senior_manager_code ) AS total_sm, COUNT(DISTINCT e.manager_code ) AS total_m, COUNT(DISTINCT e.employee_code ) AS total_e FROM company c LEFT JOIN employee e ON c.company_code=e.company_code GROUP BY c.company_code, c.founder ORDER BY c.company_code;