본문 바로가기

SQL

[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 count(*) desc // 3) 내림차순으로 정리하기

과목 별로 시작하지 않은 유저들을 세어보기

select c.title, count(*) from courses c  
inner join enrolleds e on c.course_id  = e.course_id 
where e.is_registered = 0 // 1) 등록/시작하지 않은 사람 찾기
group by c.title // 2) 과목 별로 묶기

웹개발, 앱개발 종합반의 week 별 체크인 수를 세어볼까요? 보기 좋게 정리해보기!

select c.title, c2.week, count(*) from courses c
inner join checkins c2 on c.course_id = c2.course_id
group by c.title, c2.week // 과목별, 주차별로 묶기
order by c.title, c2.week

연습4번에서, 8월 1일 이후에 구매한 고객들만 발라내어 보세요!

select c.title , c2.week, count(*) from courses c
inner join checkins c2 on c.course_id = c2.course_id
inner join orders o on c2.user_id = o.user_id // 한번 더 연결해주기
where o.created_at >= '2020-08-01' // 8월 이후
group by c.title , c2.week 
order by c.title , c2.week

 

7월10일 ~ 7월19일에 가입한 고객 중, 포인트를 가진 고객의 숫자, 그리고 전체 숫자, 그리고 비율을 보고 싶어요!

select count(pu.point_user_id) as pnt_user_cnt, // 2) 포인트를 가진 사람 숫자 ( null은 빼고 카운팅됨 )
       count(*) as tot_user_cnt, // 3) 전체 숫자
       round(count(pu.point_user_id)/count(*),2) as ratio // 4) 2)를 3)으로 나눈 비율 
       from users u 
left join point_users pu on u.user_id = pu.user_id 
where u.created_at BETWEEN '2020-07-10' and '2020-07-20' // 1) 7월 10일 ~ 7월 19일에 가입한 고객 찾기

'SQL' 카테고리의 다른 글

[SQL] 3주차 Union  (0) 2022.03.03
[SQL] 3주차 Homework  (0) 2022.03.03
[SQL] 3주차 Join  (0) 2022.03.03
[SQL] Homework 2  (0) 2022.02.24
[SQL] Quiz 2  (0) 2022.02.24