https://school.programmers.co.kr/learn/courses/30/lessons/284528
코드설명
INNER JOIN + CASE WHEN + GROUP BY + AVG 를 활용합니다.
문제의 HR_DEPARTMENT 테이블은 사용하지 않아도 됩니다.
코드
SELECT
HE.EMP_NO, HE.EMP_NAME,
CASE
WHEN AVG(HG.SCORE) >= 96 THEN 'S'
WHEN AVG(HG.SCORE) >= 90 THEN 'A'
WHEN AVG(HG.SCORE) >= 80 THEN 'B'
ELSE 'C'
END AS GRADE,
CASE
WHEN AVG(HG.SCORE) >= 96 THEN HE.SAL * 0.2
WHEN AVG(HG.SCORE) >= 90 THEN HE.SAL * 0.15
WHEN AVG(HG.SCORE) >= 80 THEN HE.SAL * 0.1
ELSE HE.SAL * 0
END AS BONUS
FROM HR_EMPLOYEES AS HE
INNER JOIN HR_GRADE AS HG
ON HE.EMP_NO = HG.EMP_NO
GROUP BY HE.EMP_NO, HE.EMP_NAME
ORDER BY HE.EMP_NO ASC