대외활동 및 인턴/빅데이터 분석 학회 BDA
SQL 문제 풀이 (BDA학회 SQL 문법 기초 연습반)
따`ddah
2025. 1. 12. 00:48
1. 제품 중에서 가장 비싼 제품을 조회해서 해당 제품이 무엇인지 출력
select *
from products
order by price desc
limit 1;
2. 카테고리별 평균 가격을 정리해서 출력
select
category,
avg(price)
from products
group by category;

3. product13 제품의 모든 order_id, customer_id, total_amount 조회
select
od.product_id,
od.order_id,
o.customer_id,
o.total_amount
from order_details od
join orders o on o.order_id = od.order_id
where od.product_id = 13;
4. 23년도 각 city의 월별 총매출
select
DATE_FORMAT(o.order_date, '%Y-%m') AS "yearMonth", ## 2023-01 이런식으로 표시됨
MONTH(order_date) month,
c.city,
sum(total_amount) as "총 매출" ## 뛰어쓰기 있을때는 ""로 표시
from orders o
join customers c on o.customer_id = c.customer_id
where YEAR(order_date) = 2023
group by DATE_FORMAT(o.order_date, '%Y-%m'), c.city, MONTH(order_date)
order by yearMonth, MONTH(order_date), c.city;
5. 구매한 총 주문 수 기준으로 상위 5명 정도의 고객의 이름, 이메일, 도시 추출
select
name, email, city, total_amount
from orders o
join customers c on c.customer_id = o.customer_id
order by o.total_amount desc
limit 5;
728x90