๐Ÿ’ SQL/Group By

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค][SQL] ์กฐ๊ฑด์— ๋งž๋Š” ์‚ฌ์› ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ (284527)

์„ ๋‹ฌ 2024. 10. 21. 16:19
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

HR_DEPARTMENT, HR_EMPLOYEES, HR_GRADE ํ…Œ์ด๋ธ”์—์„œ 2022๋…„๋„ ํ•œํ•ด ํ‰๊ฐ€ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์‚ฌ์› ์ •๋ณด๋ฅผ ์กฐํšŒํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. 2022๋…„๋„ ํ‰๊ฐ€ ์ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ์‚ฌ์›๋“ค์˜ ์ ์ˆ˜, ์‚ฌ๋ฒˆ, ์„ฑ๋ช…, ์ง์ฑ…, ์ด๋ฉ”์ผ์„ ์กฐํšŒํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

2022๋…„๋„์˜ ํ‰๊ฐ€ ์ ์ˆ˜๋Š” ์ƒ,ํ•˜๋ฐ˜๊ธฐ ์ ์ˆ˜์˜ ํ•ฉ์„ ์˜๋ฏธํ•˜๊ณ , ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ปฌ๋Ÿผ์˜ ์ด๋ฆ„์€ SCORE๋กœ ํ•ด์ฃผ์„ธ์š”.

 

ํ’€์ด

select s.SCORE, e.EMP_NO, e.EMP_NAME, e.POSITION, e.EMAIL
from HR_EMPLOYEES e 
    join (
        select EMP_NO, sum(SCORE) as SCORE
        from HR_GRADE
        group by EMP_NO
    ) s on e.EMP_NO = s.EMP_NO
order by s.SCORE desc 
limit 1
๋ฐ˜์‘ํ˜•