Skip to content
Advertisement

How to create query more than two table in SQL?

I found difficulties answering Q2.

Orders >>
OrderID, CustomerID, ProductID, Qty, OrderDatetime

Customers >>
CustomerID, CustomerName, Area, CreatedAt

Products>>
ProductID, ProductName, Category, UnitPrice

Q1) Total order value (amount) of the orders placed in the period Jan 2022, from the area Jakarta

Q2) How many orders raised for Product = “OPPO A31” in the category = “Mobile Phones” in Jan 2022? And, by how many customers in Jan 2022?

Advertisement

Answer

Here are a pair of queries for you to test:

select
  count(distinct o.orderID)
from Orders o
join Products p on o.ProductID = p.productID
where p.Category = 'Mobile Phones'
and month(p.orderdatetime) = 1
and year(p.orderdatetime) = 2022
and p.productID = 'OPPO A31';
select
  count(distinct c.CustomerID,)
from customers category
join Orders o
on c.custtomerID = o.customerID
join Products p on o.ProductID = p.productID
where p.Category = 'Mobile Phones'
and month(p.orderdatetime) = 1
and year(p.orderdatetime) = 2022
and p.productID = 'OPPO A31';
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement