T_era

[MySQL] 문제풀이 : 연습문제! 본문

Programing/Datababse

[MySQL] 문제풀이 : 연습문제!

블스뜸 2025. 3. 25. 14:22

마지막 연습 문제 !

 
다음과 같은 상품(products) 테이블과 주문(orders) 테이블이 있습니다.
products 테이블
id
name
price
1
랩톱
1200
2
핸드폰
800
3
타블렛
400
orders 테이블
id
product_id
quantity
order_date
101
1
2
2023-03-01
102
2
1
2023-03-02
103
3
5
2023-03-04
 
44.
모든 주문의 주문 ID와 주문된 상품의 이름을 나열하는 쿼리를 작성해주세요!
더보기

select

o.id,
p.name
from orders as o
join products as p on o.product_id = p.id

 
45.
총 매출(price * quantity의 합)이 가장 높은 상품의 ID와 해당 상품의 총 매출을 가져오는 쿼리를 작성해주세요!
더보기

select

o

.

product_i

,


SUM

(

p

.

price

*

o

.

quantity

)


from orders as o
join products as p on o

.

product_id

=

p

.

id
group by o

.

product_id
order

 2

desc
limit 1

46.
각 상품 ID별로 판매된 총 수량(quantity)을 계산하는 쿼리를 작성해주세요!
더보기

select

product_id,
SUM(quantity)
from orders
group by 1

47.
2023년 3월 3일 이후에 주문된 모든 상품의 이름을 나열하는 쿼리를 작성해주세요!
더보기

select

distinct p.name
from orders as o
join products as p on o.product_id = p.id
where o.order_date > '2023-03-03'

48.
가장 많이 판매된 상품의 이름을 찾는 쿼리를 작성해주세요!
더보기

select

p.name
from orders as o
join products as p on o.product_id = p.id
group by p.name
order by SUM(o.quantity) desc
limit 1

49.
각 상품 ID별로 평균 주문 수량을 계산하는 쿼리를 작성해주세요!
더보기

select

product_id,
AVG(quantity)
from orders
group by 1

50.
판매되지 않은 상품의 ID와 이름을 찾는 쿼리를 작성해주세요!
더보기

select

p.id,
p.name
from products as p
left join orders o on p.id = o.product_id
where o.product_id is null