๐Ÿ’ SQL/Group By

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ๋ถ€์„œ๋ณ„ ํ‰๊ท  ์—ฐ๋ด‰ ์กฐํšŒํ•˜๊ธฐ

์„ ๋‹ฌ 2024. 10. 15. 03:27
๋ฐ˜์‘ํ˜•

https://school.programmers.co.kr/learn/courses/30/lessons/284529

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ

HR_DEPARTMENT์™€ HR_EMPLOYEES ํ…Œ์ด๋ธ”์„ ์ด์šฉํ•ด ๋ถ€์„œ๋ณ„ ํ‰๊ท  ์—ฐ๋ด‰์„ ์กฐํšŒํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. 

 

๋ถ€์„œ๋ณ„๋กœ ๋ถ€์„œ ID, ์˜๋ฌธ ๋ถ€์„œ๋ช…, ํ‰๊ท  ์—ฐ๋ด‰์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
ํ‰๊ท ์—ฐ๋ด‰์€ ์†Œ์ˆ˜์  ์ฒซ์งธ ์ž๋ฆฌ์—์„œ ๋ฐ˜์˜ฌ๋ฆผํ•˜๊ณ  ์ปฌ๋Ÿผ๋ช…์€ AVG_SAL๋กœ ํ•ด์ฃผ์„ธ์š”.
๊ฒฐ๊ณผ๋Š” ๋ถ€์„œ๋ณ„ ํ‰๊ท  ์—ฐ๋ด‰์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

 

ํ’€์ด

select e.DEPT_ID, d.DEPT_NAME_EN, round(avg(e.SAL)) as AVG_SAL
from HR_EMPLOYEES e join HR_DEPARTMENT d on e.DEPT_ID = d.DEPT_ID
group by e.DEPT_ID
order by AVG_SAL desc
๋ฐ˜์‘ํ˜•