
4주차 3강 수업의 11) From 절에 들어가는 Subquery 연습해보기 중 질문이 있습니다.
모범 답안으로 아래와 같은 코드를 강의로 보여주셨는데요.
SELECT c,title, a.cnt_checkins, b.totalcnt, a.cnt_checkins/b.totalcnt as ratio
FROM (
select course_id, count(DISTINCT(user_id)) as cnt_checkins
from checkins
group by course_id
) a
inner join (
SELECT o.course_id, COUNT(o.user_id) as totalcnt, course_title
FROM orders o
group by o.course_id
) b on a.course_id = b.course_id
inner join courses c on a.course_id = c.course_id
어짜피 orders 테이블에 course_title이란 컬럼값을 가지고 있으니, 아래와 같이 수정해서 답을 도출해내도 무방할까요?
아니면 제가 미처 알지 못한 다른 부분 때문에 courses 테이블을 한 번 더 join 시키셨던 것인지 궁금합니다.
작성한 코드 및 에러 메세지
[ 제가 작성한 쿼리 ]
SELECT b.course_title, a.cnt_checkins, b.totalcnt, a.cnt_checkins/b.totalcnt as ratio
FROM (
select course_id, count(DISTINCT(user_id)) as cnt_checkins
from checkins
group by course_id
) a
inner join (
SELECT o.course_id, COUNT(o.user_id) as totalcnt, course_title
FROM orders o
group by o.course_id
) b on a.course_id = b.course_id
