-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy path05 - New Companies.sql
29 lines (24 loc) · 967 Bytes
/
05 - New Companies.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- ========================
-- Information
-- ========================
-- Direct Link: https://www.hackerrank.com/challenges/the-company/problem
-- Difficulty: Medium
-- Max Score: 30
-- DBMS: mySQL
-- ========================
-- Solution
-- ========================
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 c, Lead_Manager l, Senior_Manager s, Manager m, Employee e
WHERE c.company_code = l.company_code AND
l.lead_manager_code = s.lead_manager_code AND
s.senior_manager_code = m.senior_manager_code AND
m.manager_code = e.manager_code
GROUP BY c.company_code, c.founder ORDER BY c.company_code;
-- ========================
-- Explanation
-- ========================
-- DISTINCT() used to avoid duplication
-- COUNT() used to return the number of rows in a table