You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. What is the total amount each customer spent at the restaurant?
SelectS.customer_id, Sum(M.price) as Total_sales
From Menu m
Join Sales s
Onm.product_id=s.product_idGroup byS.customer_id
Answer:
Customer_id
Total_sales
A
76
B
74
C
36
Customer A, B and C spent $76, $74 and $36 respectivly.
2. How many days has each customer visited the restaurant?
Select customer_id, count(distinct(order_date)) as Times_visited
From Sales
Group by customer_id
Answer:
Customer_id
Times_visited
A
4
B
6
C
2
Customer A, B and C visited 4, 6 and 2 times respectivly.
3. What was the first item from the menu purchased by each customer?
With Rank as
(
SelectS.customer_id,
M.product_name,
S.order_date,
DENSE_RANK() OVER (PARTITION BY S.Customer_IDOrder byS.order_date) as rank
From Menu m
Join Sales s
Onm.product_id=s.product_idGroup byS.customer_id, M.product_name,S.order_date
)
Select Customer_id, product_name
From Rank
Where rank =1;
Answer:
Customer_id
product_name
A
curry
A
sushi
B
curry
C
ramen
Customer A's first order is curry and sushi.
Customer B's first order is curry.
Customer C's first order is ramen.
4. What is the most purchased item on the menu and how many times was it purchased by all customers?
Select Top 1M.Product_name , Count(S.product_id) as Times_Purchased
From Menu m
Join Sales s
Onm.product_id=s.product_idGroup byM.product_nameOrder byCount(S.product_id) desc;
Answer:
Product_name
Times_Purchased
ramen
8
Most purchased item on the menu is ramen which is 8 times.
5. Which item was the most popular for each customer?
With rank as
(
SelectS.customer_ID ,
M.product_name,
Count(S.product_id) as Count,
Dense_rank() Over (Partition by S.Customer_IDorder byCount(S.product_id) DESC ) as Rank
From Menu m
Join Sales s
Onm.product_id=s.product_idGroup byS.customer_id,S.product_id,M.product_name
)
Select Customer_id,Product_name,Count
From rank
Where rank =1;
Answer:
Customer_id
Product_name
Count
A
ramen
3
B
sushi
2
B
curry
2
B
ramen
2
C
ramen
3
Customer A and C's favourite item is ramen while customer B savours all items on the menu.
6. Which item was purchased first by the customer after they became a member?
With Rank as
(
SelectS.customer_id,
M.product_name,
S.order_date,
Dense_rank() OVER (Partition by S.Customer_idOrder byS.Order_date) as Rank
From Sales S
Join Menu M
ONm.product_id=s.product_idJOIN Members Mem
ONMem.Customer_id=S.customer_idWhereS.order_date>=Mem.join_date
)
Select Customer_id, Product_name, Order_date
From Rank
Where Rank =1;
Answer:
customer_id
product_name
order_date
A
curry
2021-01-07
B
sushi
2021-01-11
After becoming a member
Customer A's first order was curry.
Customer B's first order was sushi.
7. Which item was purchased just before the customer became a member?
With Rank as
(
SelectS.customer_id,
M.product_name,
S.Order_date,
Dense_rank() OVER (Partition by S.Customer_idOrder byS.Order_dateDESC) as Rank
From Sales S
Join Menu M
Onm.product_id=s.product_idJoin Members Mem
OnMem.Customer_id=S.customer_idWhereS.order_date<Mem.join_date
)
Select customer_ID, Product_name,Order_date
From Rank
Where Rank =1;
Answer:
customer_id
product_name
order_date
A
sushi
2021-01-01
A
curry
2021-01-01
B
sushi
2021-01-04
Before becoming a member
Customer A’s last order was sushi and curry.
Customer B’s last order wassushi.
8. What is the total items and amount spent for each member before they became a member?
SelectS.customer_id,count(S.product_id ) as Items ,Sum(M.price) as total_sales
From Sales S
Join Menu M
ONm.product_id=s.product_idJOIN Members Mem
ONMem.Customer_id=S.customer_idWhereS.order_date<Mem.join_dateGroup byS.customer_id;
Answer:
customer_id
Items
total_sales
A
2
25
B
3
40
Before becoming a member
Customer A spent $25 on 2 items.
Customer B spent $40 on 3 items.
9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier — how many points would each customer have?
With Points as
(
Select*, Case When product_id =1 THEN price*20
Else price*10
End as Points
From Menu
)
SelectS.customer_id, Sum(P.points) as Points
From Sales S
Join Points p
Onp.product_id=S.product_idGroup byS.customer_id;
Answer:
customer_id
Points
A
860
B
940
C
360
Total points for customer A, B and C are 860, 940 and 360 respectivly.
10. In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi — how many points do customer A and B have at the end of January?
WITH dates AS
(
SELECT*,
DATEADD(DAY, 6, join_date) AS valid_date,
EOMONTH('2021-01-31') AS last_date
FROM members
)
SelectS.Customer_id,
SUM(
Case
When m.product_ID=1 THEN m.price*20
When S.order_date between D.join_dateandD.valid_date Then m.price*20
Else m.price*10
END
) as Points
From Dates D
join Sales S
OnD.customer_id=S.customer_idJoin Menu M
OnM.product_id=S.product_idWhereS.order_date<d.last_dateGroup byS.customer_id;
Answer:
Customer_id
Points
A
1370
B
820
Total points for Customer A and B are 1,370 and 820 respectivly.