«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Recent Posts
Today
Total
관리 메뉴

짜리몽땅 매거진

[SQL] solvesql 코딩테스트 연습6 본문

Data/SQL

[SQL] solvesql 코딩테스트 연습6

쿡국 2024. 4. 22. 15:39

문제1.  우리 플랫폼에 정착한 판매자 2

출처 : solvesql 연습문제

문제 정보 : 난이도 2 / 정답률 28.17%

https://solvesql.com/problems/settled-sellers-2/

 

https://solvesql.com/problems/settled-sellers-2/

 

solvesql.com

 

정답 쿼리

select seller_id, count(distinct order_id) as orders
from olist_order_items_dataset
where price >= 50
group by seller_id
having orders >=100
order by orders desc

 

문제 해설

 

1. 주요 포인트 1 - 조건에 대하여 where절과 having절의 차이

where price >= 50

having orders >=100

 

간단하게 sql문법에서 where절과 having절의 차이를 정리하자면 아래와 같다.

 

  • having절은 그룹을 필터링하는 데 사용된다.
  • where절을 행을 필터링 하는데 사용된다.

문제에서는 '상품 가격이 50달러 이상인 주문이 100건 이상 들어온 판매자 리스트를 주문 건수가 많은 순서대로 출력하는 쿼리를 작성해주세요.' 라고 하였는데, 이는 곧 마지막에 판매자 id 별로 그룹핑을 하라는 의미이다. 그 중에서도 각 판매자 별로 주문이 100건 이상 들어온 판매자만 추려야 하므로 그룹을 필터링하는데 사용되는 having절 뒤에 작성해야 한다. 50달러 이상인 주문에 대해서는 각 행을 우선적으로 필터링하는 것이므로 where절 뒤에 작성한다.

 


문제2.  할부는 몇 개월로 해드릴까요

출처 : solvesql 데이터리안 sql캠프 입문반

문제 정보 : 난이도 3 / 정답률 16.39%

https://solvesql.com/problems/installment-month/

 

https://solvesql.com/problems/installment-month/

 

solvesql.com

 

정답 쿼리

select payment_installments, count(distinct order_id) as order_count,
min(payment_value) as min_value, max(payment_value) as max_value,
avg(payment_value) as avg_value
from olist_order_payments_dataset
where payment_type = 'credit_card'
group by payment_installments

 

문제 해설

 

1. 주요 포인트 1 - select절 집계함수

select payment_installments, count(distinct order_id) as order_count,
min(payment_value) as min_value, max(payment_value) as max_value,
avg(payment_value) as avg_value

 

각 칼럼 별로 할부 개월 수 별 주문 수, 최소 주문금액, 최대 주문금액, 평균 주문금액 값을 원하므로 적절한 집계함수를 입력해준다.