SQL 썸네일형 리스트형 [SQL] 4주차 With / 문자열 / Case With → 깔끔한 쿼리문 정리하기 위해 사용 select c.title, a.cnt_checkins, b.cnt_total, (a.cnt_checkins/b.cnt_total) as ratio from ( select course_id, count(distinct(user_id)) as cnt_checkins from checkins group by course_id ) a inner join ( select course_id, count(*) as cnt_total from orders group by course_id ) b on a.course_id = b.course_id inner join courses c on a.course_id = c.course_id 위의 코드를 with절을 사용해서 .. 더보기 [SQL] 4주차 Subquery Subquery → 하나의 SQL 쿼리 안에 또다른 SQL 쿼리가 있는 것을 의미합니다. Where절에 들어가는 Subquery → where 필드명 in (subquery) select u.user_id, u.name, u.email from users u where u.user_id in ( select user_id from orders where payment_method = 'kakaopay' ) // (1) from 실행: users 데이터를 가져와줌 (2) Subquery 실행: 해당되는 user_id의 명단을 뽑아줌 (3) where .. in 절에서 subquery의 결과에 해당되는 'user_id의 명단' 조건으로 필터링 해줌 (4) 조건에 맞는 결과 출력 Select절에 들어가는 Su.. 더보기 [SQL] 3주차 Union Select를 두 번 할 게 아니라, 한번에 모아서 보고싶은 경우 ( select '7월' as month, c.title, c2.week, count(*) as cnt from checkins c2 inner join courses c on c2.course_id = c.course_id inner join orders o on o.user_id = c2.user_id where o.created_at < '2020-08-01' group by c2.course_id, c2.week order by c2.course_id, c2.week ) union all ( select '8월' as month, c.title, c2.week, count(*) as cnt from checkins c2 inner .. 더보기 [SQL] 3주차 Homework enrolled_id별 수강완료(done=1)한 강의 갯수를 세어보고, 완료한 강의 수가 많은 순서대로 정렬해보기. user_id도 같이 출력되어야 한다. 조인해야 하는 테이블: enrolleds, enrolleds_detail 조인하는 필드: enrolled_id select e.enrolled_id, e.user_id, count(*) as max_count from enrolleds e inner join enrolleds_detail ed on e.enrolled_id = ed.enrolled_id where ed.done = 1 group by e.enrolled_id, e.user_id order by max_count desc 더보기 [SQL] 3주차 Quiz 결제 수단 별 유저 포인트의 평균값 구해보기 select o.payment_method, round(avg(pu.point),0) // 2)포인트의 평균 구하기 from orders o inner join point_users pu on o.user_id = pu.user_id group by o.payment_method // 1)결제수단별로 묶고 결제하고 시작하지 않은 유저들을 성씨별로 세어보기 select u.name, count(*) from enrolleds e inner join users u on e.user_id = u.user_id where is_registered = 0 // 1) 등록/시작하지 않은 유저만 찾기 group by u.name // 2) 성씨별로 묶기 order by c.. 더보기 [SQL] 3주차 Join Join → 두 테이블의 공통된 정보 (key값)를 기준으로 테이블을 연결해서 한 테이블처럼 보는 것을 의미 Left Join ~ on select * from users u left join point_users p on u.user_id = p.user_id; ▶ 어떤 데이터는 필드가 채워져있지만, 어떤 데이터는 비어있는 필드가 있다. ( null ) 어디에 → 뭐를 붙일건지, 순서가 중요 ex) null값은 회원이지만 수강을 등록/시작하지 않아 포인트가 없는 경우 비어있는 필드 찾기 : where 필드명 is null / 비어있는 필드 빼고 찾기 is not null Inner join ~ on select * from users u inner join point_users p on u.user_i.. 더보기 [SQL] Homework 2 네이버 이메일을 사용하여 앱개발 종합반을 신청한 주문의 결제수단별 주문건수 세어보기 select payment_method , count(*) from orders where email like '%naver.com' and course_title = '앱개발 종합반' group by payment_method 더보기 [SQL] Quiz 2 1)앱개발 종합반의 결제수단별 주문건수 세어보기 select payment_method , count(*) from orders where course_title = '앱개발 종합반' group by payment_method 2)Gmail 을 사용하는 성씨별 회원수 세어보기 select name, count(*) from users where email like '%gmail.com' group by name 3)course_id별 '오늘의 다짐'에 달린 평균 like 개수 구해보기 select avg(likes) from checkins group by course_id 더보기 이전 1 2 다음