๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

SQL

[SQL] ์—ฐ์†์ ์ธ JOIN ๊ตฌ๋ฌธ์œผ๋กœ ํ•ด๊ฒฐํ•˜๊ธฐ(HackerRank - New Companies ๋ฌธ์ œ)

๋ฐ˜์‘ํ˜•

๐Ÿ”Š ๋ณธ ํฌ์ŠคํŒ…์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ž๋ฃŒ์™€ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค. ๋” ๋‹ค์–‘ํ•œ SQL ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์‹œ๋ ค๋ฉด

HackerRank ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ•ด ๋ณด์„ธ์š”!

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ•ด์ปค๋žญํฌ ๋ฌธ์ œ์˜ New Companies ๋ฌธ์ œ ํ’€์ด์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ํ•„์ž๊ฐ€ ์ง์ ‘ ํ’€์ดํ–ˆ๋˜ ์—ฐ์†์ ์ธ JOIN ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ Discussion์— ์กด์žฌํ–ˆ๋˜ ๋‹ค๋ฅธ ๋ถ„์˜ JOIN ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ํ’€์ด๋„ ์†Œ๊ฐœํ•ด๋ณด๋ ค ํ•œ๋‹ค. ์ด๋ฒˆ ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ” ๊ฐœ์ˆ˜๋„ ์—ฌ๋Ÿฌ๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ํฌ์ŠคํŒ…์—์„œ ์†Œ๊ฐœํ•œ๋‹ค๋ฉด ๋„ˆ๋ฌด ๋ฒˆ์žกํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ดํ•ด๋Š” ์›๋ณธ ๋ฌธ์ œ์ธ ์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์ž.

 

์›๋ณธ ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ๋‚˜๋ฉด ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์‚ฌํ•ญ์ด ์ •ํ™•ํžˆ ๋ฌด์—‡์ธ์ง€ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ Company์˜ Company_code, Founder, ๊ฐ ํšŒ์‚ฌ์˜ Lead Manager, Senior Manager, Manager, Employee ๋ช…์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ด ๋•Œ ๋ฐ์ดํ„ฐ ์ •๋ ฌ ๊ธฐ์ค€์€ Company์˜ Company_code ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š”๋ฐ, Company_code๊ฐ’์ด ๋ฌธ์ž์—ด ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์— ์ˆซ์ž๊ฐ€ ๋“ค์–ด์žˆ๋”๋ผ๋„ ๋ฌธ์ž์—ด ์ทจ๊ธ‰์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด Company_code ๊ฐ’๋“ค์ด ์ฃผ์–ด์กŒ๋‹ค๊ณ  ์น˜์ž.

 

Code
A_10
A_1
B_3
B_12

 

์œ„ ๊ฐ’๋“ค์„ ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ํ˜•ํƒœ๋กœ ์ •๋ ฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ ฌ์ด ๋œ๋‹ค.

 

Code
A_1
A_10
B_12
B_3

1. ์—ฐ์†์ ์ธ INNER JOIN ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋ณด๊ธฐ

๋ฌธ์ œ๋ฅผ ํ’€์ดํ•  ๋•Œ ๊ฐ€์žฅ ์ง‘์ค‘ํ–ˆ๋˜ ๋ถ€๋ถ„์€ ๋ชจ๋“  ํ…Œ์ด๋ธ”์— ๊ณตํ†ต์ ์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ์นผ๋Ÿผ์ด Company_code ์˜€๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ Company_code ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๊ณ„์†์ ์œผ๋กœ INNER JOIN์„ ์ˆ˜ํ–‰ํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  INNER JOIN ์‹œํ‚ฌ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ ๋งค๋‹ˆ์ €๋“ค, ์ง์›์˜ COUNT ๊ฐ’์„ ์ง‘๊ณ„ํ–ˆ๋‹ค. ์ฐธ๊ณ ๋กœ DISTINCTํ•œ ์ด์œ ๋Š” ๋ฌธ์ œ์—์„œ ์ค‘๋ณต๋œ(duplicated) ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ๋ช…์‹œํ•ด์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

SELECT company.company_code, company.founder, lead.lead_count, senior.senior_count,
       manager.manager_count, employee.employee_count
FROM company
 INNER JOIN (SELECT company_code, COUNT(DISTINCT(lead_manager_code)) AS lead_count
             FROM lead_manager
             GROUP BY company_code) lead ON company.company_code = lead.company_code
 INNER JOIN (SELECT company_code, COUNT(DISTINCT(senior_manager_code)) AS senior_count
             FROM senior_manager
             GROUP BY company_code) senior ON company.company_code = senior.company_code
 INNER JOIN (SELECT company_code, COUNT(DISTINCT(manager_code)) AS manager_count
             FROM manager
             GROUP BY company_code) manager ON company.company_code = manager.company_code
 INNER JOIN (SELECT company_code, COUNT(DISTINCT(employee_code)) AS employee_count
             FROM employee
             GROUP BY company_code) employee ON company.company_code = employee.company_code
ORDER BY company.company_code

2. WHERE ๊ตฌ๋ฌธ๋งŒ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋ณด๊ธฐ

์ด ํ’€์ด๋Š” ๋ฌธ์ œ์˜ Discussion ํƒญ์—์„œ ๋ณด๊ฒŒ ๋œ ํ’€์ด์ธ๋ฐ, INNER JOIN์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค๋Š” ์ ์ด ์ธ์ƒ์ ์ด์—ˆ๋‹ค. ๋˜ ๊ทธ๋™์•ˆ์— FROM ์ ˆ ๋’ค์—๋Š” 1๊ฐœ์˜ ํ…Œ์ด๋ธ”๋งŒ ๋‚˜์™€์•ผ ํ•œ๋‹ค๊ณ  ๋‹น์—ฐํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ์ด๋Ÿฌํ•œ ๊ณ ์ •๊ด€๋…(?)์„ ๊นจ๋œจ๋ฆฌ๋Š” ํ’€์ด์˜€๊ธฐ๋„ ํ–ˆ๋‹ค. ํ’€์ด๋ฅผ ์‚ดํŽด๋ณด์ž.

 

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

 

ํ’€์ด๋ฅผ ๋ณด๊ฒŒ๋˜๋ฉด FROM ์ ˆ์— 5๊ฐœ์˜ ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ๋ช…์‹œํ•ด์ค„ ์ˆ˜ ์žˆ์Œ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  WHERE ์ ˆ์—๋Š” ์•ž์„œ ๋ช…์‹œํ•ด์ฃผ์—ˆ๋˜ 5๊ฐœ์˜ ํ…Œ์ด๋ธ”๋“ค์ด ์„œ๋กœ ๊ตฌํ•˜๊ณ  ์‹ถ์–ดํ•˜๋Š” ๋งค๋‹ˆ์ €, ์ง์›๋“ค code ๊ฐ’์ด ๊ฐ™๋„๋ก ์กฐ๊ฑด์„ ๋ถ™์—ฌ์ฃผ์—ˆ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ตฌํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ™์€ Company_code์— ์†ํ•˜๋Š” ๋งค๋‹ˆ์ €๋“ค๊ณผ ์ง์›๋“ค์˜ ๋ช…์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค ํ•˜๋Š” ๊ฒƒ์ด๊ณ  ์ถœ๋ ฅ ์‹œ ๊ทธ Company์˜ Founder๋„ ์ถœ๋ ฅํ•ด์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Company_code, Founder ์ด 2๊ฐœ์˜ ์นผ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘์„ ํ•ด์•ผ ํ•œ๋‹ค.

๋ฐ˜์‘ํ˜•