[250509] 사전캠프 5일차 | SQL 4주차 복습

곽다혜·2025년 5월 9일

SQL 4주차 복습

subquery 실습

[음식점의 평균 단가별 segmentation을 진행하고, 그룹에 따라 수수료 연산하기]
<수수료 구간>
-5000원 미만: 0.5%
-20000원 미만: 1%
-30000원 미만: 2%
-30000원 초과: 3%

<정답>

select restaurant_name,
price_per_plate*ratio_of_add "수수료"
from
(
select restaurant_name,
case when price_per_plate<5000 then 0.005
when price_per_plate between 5000 and 19999 then 0.01
when price_per_plate between 20000 and 29999 then 0.02
else 0.03 end ratio_of_add,
price_per_plate
from
(
select restaurant_name,
avg(price/quantity) price_per_plate
from food_orders
group by 1
) a
)b

[음식점의 총 주문수량과 주문 금액을 연산하고, 주문 수량을 기반으로 할인률 구하기]
<할인 조건>
-수량이 5개 이하: 10%
-수량이 15개 초과, 총 주문금액이 300000 이상: 0.5%
-그 외에는 일괄 1%

<정답>

select restaurant_name,
case when sum_quantity<=5 then 0.1
when sum_quantity>15 and sum_price>=300000 then 0.005
else 0.01 end 'ratio_of_add'
from
(
select restaurant_name,
sum(quantity) sum_quantitiy,
sum(price) sum_price
from food_orders
group by 1
) a

Join 실습

[한국 음식의 주문별 결제 수단과 수수료율 조회하기]

select f.order_id,
f.restaurant_name,
f.price,
p.pay_type,
p.vat
from food_orders f left join payments p on f.order_id=p.order_id
where cuisine_type='Korean'

[4주차 숙제-식당별 평균 음식 주문 금액과 주문자의 평균 연령을 기반으로 segmentation 하기]

select restaurant_name,
case when price<= 5000 then 'price_group1'
when price>5000 and price<=10000 then 'price_group2'
when price>10000 and price<=30000 then 'price_group3'
when price>30000 then 'price_group4' end price_group,
case when age<30 then 'age_group1'
when age between 30 and 39 then 'age_group2'
when age between 40 and 49 then 'age_group3'
else 'age_group4' end age_group
from
(
select f.restaurant_name,
avg(price) price,
avg(age) age
from food_orders f inner join customers c on f.customer_id=c.customer_id
group by 1
) a
order by 1
profile
데이터 분석 적응기

0개의 댓글