작성
·
260
0
-- 4. 강의별 평균 학점, 꿀강의 TOP 3 조회
SELECT courses.title AS "강의명", ROUND(AVG(grade_points.point), 2) AS "평균 학점"
FROM courses
JOIN grades ON grades.course_id = courses.id
JOIN grade_points ON grade_points.grade = grades.grade
GROUP BY courses.title
ORDER BY AVG(point) DESC
LIMIT 3
;
-- 서브쿼리 이용 답
SELECT courses.title AS "강의명", ggul_courses.round AS "평균 학점"
FROM courses
JOIN (
-- 강의별_평균학점
SELECT course_id, ROUND(AVG(grade_points.point), 2)
FROM grades
JOIN grade_points ON grade_points.grade = grades.grade
GROUP BY course_id
HAVING AVG(point) > (
-- 모든_강의의_평균_학점
SELECT AVG(point)
FROM grades
JOIN grade_points ON grade_points.grade = grades.grade
)
ORDER BY AVG(point) DESC
LIMIT 3
) AS ggul_courses ON ggul_courses.course_id = courses.id
;
일반 JOIN이용과 서브쿼리 이용 결과가 다르게 출력되는데
서브쿼리 안에서 정렬을 했지만 최종 결과는 정렬이 되지 않네요?
왜 그런건가요..? 이러면 JOIN이 더 나은 걸까요?