섭섭의 공부/SQL

[22.01.16] HackerRank 문제 풀이

seobbseob 2022. 1. 16. 08:15

New Companies

/*
1. 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
2. Order your output by ascending company_code.
3. The company_code is string
4. The tables may contain duplicate records.
*/

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
    inner join employee e on e.company_code = c.company_code
group by c.company_code, c.founder
order by c.company_code;

중간에 있던 테이블은 쓸 필요가 없다.

중복으로 개수를 세는 것을 피하기 위해 distinct를 사용하고, group by로 묶어 각 회사의 직급별 사원 id의 수를 세준다.

그리고 원래의 company_code와 founder를 select하고 싶음으로 group by에 두 조건 모두 넣는다.

 

(문제 출처 - https://www.hackerrank.com/domains/sql)