Products 테이블:
| ProductID | ProductName | Category | Price |
|---|---|---|---|
| 1 | Laptop | Electronics | 1000 |
| 2 | Smartphone | Electronics | 800 |
| 3 | Headphones | Electronics | 150 |
| 4 | Coffee Maker | Home | 200 |
| 5 | Blender | Home | 100 |
Orders 테이블:
| OrderID | ProductID | OrderDate | Quantity | CustomerID |
|---|---|---|---|---|
| 101 | 1 | 2024-02-01 | 2 | 1 |
| 102 | 3 | 2024-02-02 | 1 | 2 |
| 103 | 2 | 2024-02-03 | 1 | 1 |
| 104 | 4 | 2024-02-04 | 3 | 3 |
| 105 | 1 | 2024-02-05 | 1 | 2 |
| 106 | 5 | 2024-02-06 | 2 | 3 |
Customers 테이블:
| CustomerID | CustomerName | Country |
|---|---|---|
| 1 | Alice | USA |
| 2 | Bob | UK |
| 3 | Charlie | USA |
각 고객이 구매한 모든 제품의 총 금액을 계산하고, 고객 이름, 총 구매 금액, 주문 수를 출력하는 SQL 쿼리를 작성해주세요.
| CustomerName | TotalAmount | OrderCount |
|---|---|---|
| Alice | 2600 | 3 |
| Bob | 950 | 2 |
| Charlie | 800 | 2 |
각 제품 카테고리별로 가장 많이 팔린 제품의 이름과 총 판매량을 조회하는 SQL 쿼리를 작성해주세요.
| Category | Top_Product | TotalSold |
| --- | --- | --- |
| Electronics | Laptop | 3 |
| Home | Coffee Maker | 3 |
select CustomerName, sum(Price*Quantity), count(1)
customers c
join orders o on c.CustomerID = o.CustomerID
join products p on o.ProductID = p.productID
group by CustomerName
select c.CustomerName, sum(p.Price * o.Quantity) as TotalAmount, count(o.OrderID) as OrderCount
from customers c
join orders o on c.CustomerID = o.CustomerID
join products p on o.ProductID = p.productID
group by c.CustomerName
select Category, ProductName, sum(Quantity)
products p join orders o on p.productID = o.ProductID
group by Category, ProductName
having sum(Quantity) = 최대 sum(Quantity)
select sum(o2.Quantity)
from products p2 join orders o2 on p2.productID = o2.ProductID
where p2.Category = p.Category
group by p2.Category, p2.ProductName
order by sum(o2.Quantity) desc
limit 1
select p.Category, p.ProductName, sum(o.Quantity) as TotalSold
from products p join orders o on p.productID = o.ProductID
group by p.Category, p.ProductName
having sum(o.Quantity) = (
select sum(o2.Quantity)
from products p2 join orders o2 on p2.productID = o2.ProductID
where p2.Category = p.Category
group by p2.Category, p2.ProductName
order by sum(o2.Quantity) desc
limit 1
)