카테고리 없음

스파르타 퀘스트 11) 마지막 연습 문제 !

kimjunki-8 2024. 9. 27. 15:22
  1. 모든 주문의 주문 ID와 주문된 상품의 이름을 나열하는 쿼리를 작성해주세요!
  2. 총 매출(price * quantity의 합)이 가장 높은 상품의 ID와 해당 상품의 총 매출을 가져오는 쿼리를 작성해주세요!
  3. 각 상품 ID별로 판매된 총 수량(quantity)을 계산하는 쿼리를 작성해주세요!
  4. 2023년 3월 3일 이후에 주문된 모든 상품의 이름을 나열하는 쿼리를 작성해주세요!
  5. 가장 많이 판매된 상품의 이름을 찾는 쿼리를 작성해주세요!
  6. 각 상품 ID별로 평균 주문 수량을 계산하는 쿼리를 작성해주세요!
  7. 판매되지 않은 상품의 ID와 이름을 찾는 쿼리를 작성해주세요!

1. select p.id, p.name
from products p inner join orders o on p.id = o.product_id
2. select p.id '상품 ID', p.name '상품', p.price*o.quantity '총 매출'
from products p inner join orders o on p.id = o.product_id
order by p.price*o.quantity desc
limit 1
3. select p.id '상품 ID', p.name '상품', o.quantity '총 수량'
from products p inner join orders o on p.id = o.product_id
order by p.price*o.quantity desc
4. select p.id '상품 ID', p.name '상품', o.quantity '총 수량', o.order_date '주문 날짜'
from products p inner join orders o on p.id = o.product_id
where o.order_date > '2023-03-03'
5. select p.id '상품 ID', p.name '상품', o.quantity '총 수량', o.order_date '주문 날짜'
from products p inner join orders o on p.id = o.product_id
order by o.quantity desc
limit 1

6. select p.id '상품 ID', p.name, avg(o.quantity) '평균 수량'
from products p inner join orders o on p.id = o.product_id
group by 1, 2

7. select p.id '상품 ID', p.name '상품'
from products p inner join orders o on p.id = o.product_id
where p.id is null

Done!