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

SQL

[SQL] HAVING, WHERE, FROM ์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ ์ด์šฉํ•˜๊ธฐ(HackerRank - Top Earners ๋ฌธ์ œ)

๋ฐ˜์‘ํ˜•

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

 

HackerRank ์‚ฌ์ดํŠธ

 

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” HackerRank ์‚ฌ์ดํŠธ์˜ SQL ๋ฌธ์ œ์ค‘ 'Top Earners' ๋ฌธ์ œ์— ๋Œ€ํ•œ ์„œ๋กœ ๋‹ค๋ฅธ 3๊ฐ€์ง€ ํ’€์ด๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์†Œ๊ฐœํ•ด๋ณด๋ ค ํ•œ๋‹ค. ์ด 3๊ฐ€์ง€ ํ’€์ด์˜ ๊ณตํ†ต์ ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์ธ ๋ฐ˜๋ฉด ์ฐจ์ด์ ์€ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ SQL ๊ตฌ๋ฌธ์˜ ์–ด๋Š ์œ„์น˜ ์ฆ‰, HAVING ๋‹ค์Œ์ธ์ง€, WHERE ๋‹ค์Œ์ธ์ง€, FROM ๋‹ค์Œ์ธ์ง€์— ๋”ฐ๋ผ ์กฐ๊ธˆ์”ฉ ๋‹ฌ๋ผ์ง„๋‹ค๋Š” ์ ์ด๋‹ค.

 

์šฐ์„  ํ’€์ดํ•  ๋ฌธ์ œ์™€ ๋ฌธ์ œ๊ฐ€ ์š”๊ตฌํ•˜๋Š” ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž. ๋ฌธ์ œ ์›๋ณธ์„ ๋ณด๊ธฐ ์œ„ํ•ด์„œ๋Š” ์—ฌ๊ธฐ๋ฅผ ํด๋ฆญํ•ด ํ™•์ธํ•ด๋ณด์ž. 

 

๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”

 

์œ„ ํ…Œ์ด๋ธ”์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฌธ์ œ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

We define an employee's total earnings to be their monthly  'Salary * Months' worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as  space-separated integers.
([๊ฐ„๋žตํ•˜๊ฒŒ ํ•ด์„ํ•˜์ž๋ฉด], ์ฃผ์–ด์ง„ ํ…Œ์ด๋ธ”์˜ Salary ์นผ๋Ÿผ๊ณผ Months ์นผ๋Ÿผ์„ ๊ณฑํ•œ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์ธ Earnings๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Earnings์˜ ๊ฐ€์žฅ ๋†’์€ ๊ฐ’๊ณผ ๊ทธ ๋†’์€ ๊ฐ’์„ ๊ฐ–๋Š” ์ง์›๋“ค์˜ ๋ช…์ˆ˜(count)๋ฅผ ์ถœ๋ ฅ์‹œ์ผœ๋ผ.)

 

๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ์ถœ๋ ฅํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

Input/Output ์˜ˆ์‹œ์™€ ์„ค๋ช…

 

๋ฌธ์ œํ’€์ด ๋ฐฉ๋ฒ•์— ์ƒ๊ด€์—†์ด ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์ƒ๊ฐํ•ด์•ผ ํ•  ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. ์šฐ์„  Earnings์˜ ๊ฐ€์žฅ ๋†’์€ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ
  2. Earnings์˜ ๊ฐ€์žฅ ๋†’์€ ๊ฐ’์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ row๋ฅผ count ํ•˜๊ธฐ

 

์ด์ œ 3๊ฐ€์ง€์˜ ์„œ๋กœ ๋‹ค๋ฅธ ํ’€์ด๋ฒ•์„ ์‚ดํŽด๋ณด์ž.

1. HAVING ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ด์šฉํ•˜๊ธฐ

HAVING์€ GROUP BY ๊ฐ€ ์„ ํ–‰์œผ๋กœ ๋“ฑ์žฅํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•์œผ๋กœ, ํŠน์ • ์นผ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ•‘ ํ–ˆ์„ ๋•Œ, ์ถ”๊ฐ€์ ์ธ ์กฐ๊ฑด์„ ๋‹ฌ์•„ ํ•„ํ„ฐ๋ง ํ•ด์ฃผ๋Š” ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. HAVING์˜ ์ž์„ธํ•œ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์€ ์—ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ•˜์ž. HAVING ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

SELECT (months*salary) AS earnings, COUNT(*)
FROM Employee
GROUP BY earnings
HAVING earnings = (SELECT MAX(months*salary)
                   FROM Employee)

 

์ด์ „์— ์–ธ๊ธ‰ํ•˜๊ธฐ๋„ ํ–ˆ์ง€๋งŒ WHERE์€ SELECT ~ FROM ์‚ฌ์ด์—์„œ ์ƒˆ๋กญ๊ฒŒ ์ •์˜๋œ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜์ธ (์—ฌ๊ธฐ์„œ๋Š”) earnings ๋ผ๋Š” ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์— ๋Œ€ํ•ด ์กฐ๊ฑด์„ ๋‹ฌ ์ˆ˜ ์—†์ง€๋งŒ GROUP BY๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค. ๋”ฐ๋ผ์„œ ์œ„์™€ ๊ฐ™์€ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

2. WHERE ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ด์šฉํ•˜๊ธฐ

๋‹ค์Œ์€ WHERE ์ ˆ์— ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, ๋จผ์ € ํ•ด๋‹น ํ’€์ด๋ถ€ํ„ฐ ์‚ดํŽด๋ณด์ž.

 

SELECT (months*salary) AS earnings, COUNT(*)
FROM Employee
WHERE (months*salary) = (SELECT MAX(months*salary) FROM Employee)
GROUP BY earnings

 

1๋ฒˆ๊ณผ ๋‹ค๋ฅธ์ ์ด ๋ฌด์—‡์ธ์ง€ ๋ณด์ด๋Š”๊ฐ€? WHERE ์ ˆ์„ ์‚ดํŽด๋ณด๋ฉด months*salary ๋ผ๊ณ  ๋˜์–ด ์žˆ๋‹ค. ์ด๋Š” ๊ธฐ์กด์˜ ๋ณ€์ˆ˜๋งŒ์„ ํ™œ์šฉํ•ด ์—ฐ์‚ฐ์„ ํ–ˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค. ์ฆ‰, SELECT ~ FROM ์ ˆ ์‚ฌ์ด์—์„œ ์ •์˜๋˜์–ด ์žˆ๋Š” earnings ๋ผ๋Š” ์ƒˆ๋กญ๊ฒŒ ๋งŒ๋“ค์–ด์ง„ ์นผ๋Ÿผ์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ WHERE ์ ˆ์—์„œ ๊ธฐ์กด์— ์กด์žฌํ–ˆ๋˜ months ์™€ salary ์นผ๋Ÿผ์„ ์‚ฌ์šฉํ•ด ์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•ด์ฃผ์—ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋งŒ์•ฝ WHERE ์ ˆ์—์„œ ์ƒˆ๋กœ์šด ์นผ๋Ÿผ์„ ๋งŒ๋“ค์–ด ์กฐ๊ฑด์„ ๋‹ฌ์•„์ฃผ๋ ค๋ฉด ์œ„์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

3. FROM ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ด์šฉํ•˜๊ธฐ

์ด๋ฒˆ ํ’€์ด๋Š” FROM ๊ณผ ์„œ๋ธŒ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋˜ earnings ์˜ '์ตœ๋Œ“๊ฐ’'์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค๋Š” ์ ์—์„œ ORDER BY ์™€ LIMIT ์„ ์‚ฌ์šฉํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

SELECT sub.earnings, COUNT(*)
FROM (SELECT *, (months*salary) AS earnings
      FROM Employee) sub
GROUP BY sub.earnings
ORDER BY sub.earnings DESC
LIMIT 1

 

์ด๋ฒˆ ๋ฌธ์ œ์—์„œ ๋˜ ํ•œ ๊ฐ€์ง€ ์–ป์–ด๊ฐˆ ์ ์€ ์ตœ๋Œ“๊ฐ’ ๋˜๋Š” ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•  ๋•Œ ORDER BY ์™€ LIMIT 1 ์„ ์ ์ ˆํžˆ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

ORDER BY price ASC LIMIT 1    => price๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๊ฐ’ = price์˜ ์ตœ์†Ÿ๊ฐ’(์ฐธ๊ณ ๋กœ ASC๋Š” ๋””ํดํŠธ๊ฐ’์ด๋ฏ€๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ)
ORDER BY price DESC LIMIT 1    => price๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ๊ฐ’ = price์˜ ์ตœ๋Œ“๊ฐ’    

 

๋ฐ˜์‘ํ˜•