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
Danny wants to use the data to answer a few simple questions about his customers, especially about their visiting patterns, how much money they’ve spent and also which menu items are their favourite. Having this deeper connection with his customers will help him deliver a better and more personalised experience for his loyal customers.
📂 Dataset
Danny has shared with you 3 key datasets for this case study:
sales
View table
The sales table captures all customer_id level purchases with an corresponding order_date and product_id information for when and what menu items were ordered.
customer_id
order_date
product_id
A
2021-01-01
1
A
2021-01-01
2
A
2021-01-07
2
A
2021-01-10
3
A
2021-01-11
3
A
2021-01-11
3
B
2021-01-01
2
B
2021-01-02
2
B
2021-01-04
1
B
2021-01-11
1
B
2021-01-16
3
B
2021-02-01
3
C
2021-01-01
3
C
2021-01-01
3
C
2021-01-07
3
menu
View table
The menu table maps the product_id to the actual product_name and price of each menu item.
product_id
product_name
price
1
sushi
10
2
curry
15
3
ramen
12
members
View table
The final members table captures the join_date when a customer_id joined the beta version of the Danny’s Diner loyalty program.
customer_id
join_date
A
1/7/2021
B
1/9/2021
🚀 Solutions
Q1. What is the total amount each customer spent at the restaurant?
selecta.customer_id, sum(b.price) as total_spend
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idgroup bya.customer_id;
customer_id
total_spent
A
76
B
74
C
36
Q2. How many days has each customer visited the restaurant?
select customer_id, count(distinct order_date) as total_days
fromdannys_diner.salesgroup by customer_id;
customer_id
total_days
A
4
B
6
C
2
Q3. What was the first item from the menu purchased by each customer?
with ordered_sales as(
selecta.customer_id, a.order_date, a.product_id,b.product_name,
dense_rank() over(partition by a.customer_idorder bya.order_date) ranking
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_id
)
select distinct customer_id, product_name
from ordered_sales
where ranking =1;
customer_id
product_name
A
sushi
A
curry
B
curry
C
ramen
Q4. What is the most purchased item on the menu and how many times was it purchased by all customers?
selectb.product_name, count(a.order_date) as item_bought
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idgroup byb.product_nameorder by item_bought desclimit1;
product_name
item_bought
ramen
8
Q5. Which item was the most popular for each customer?
with temp as(
selecta.customer_id, b.product_name, count(a.order_date) as item_bought_count,
dense_rank() over (partition by a.customer_idorder bycount(a.order_date) desc) ranking
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idgroup bya.customer_id, b.product_name
)
SELECT customer_id, product_name, item_bought_count
FROM temp
WHERE ranking =1;
customer_id
product_name
item_bought_count
A
ramen
3
B
curry
2
B
sushi
2
B
ramen
2
C
ramen
3
Q6. Which item was purchased first by the customer after they became a member?
with temp_cte as(
selecta.customer_id, a.order_date,b.product_name,
dense_rank() over(partition by a.customer_idorder bya.order_date) ranking
fromdannys_diner.sales a
inner joindannys_diner.members c
ona.customer_id=c.customer_idleft joindannys_diner.menu b
ona.product_id=b.product_idwherea.order_date>=c.join_date
)
select customer_id, order_Date, product_name
from temp_cte where ranking =1;
customer_id
order_Date
product_name
A
2021-01-07
curry
B
2021-01-11
sushi
Q7. Which item was purchased just before the customer became a member?
with temp_cte as(
selecta.customer_id, a.order_Date, b.product_name,
dense_rank() over(partition by a.customer_idorder by order_date desc) ranking
fromdannys_diner.sales a
inner joindannys_diner.members c
ona.customer_id=c.customer_idleft joindannys_diner.menu b
ona.product_id=b.product_idwherea.order_date<c.join_date
)
select customer_id, order_date,product_name
from temp_cte where ranking =1;
customer_id
order_date
product_name
A
2021-01-01
sushi
A
2021-01-01
curry
B
2021-01-04
sushi
Q8. What is the total items and amount spent for each member before they became a member?
selecta.customer_id, count(a.product_id) as total_items, sum(b.price) as total_amount
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idleft joindannys_diner.members c
ona.customer_id=c.customer_idwherea.order_date<c.join_dategroup bya.customer_id;
customer_id
total_items
total_amount
A
2
25
B
3
40
Q9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
with pointer_cte as (
select product_id,
case when product_id =1 then price*20
else price*10 end as multiplier
fromdannys_diner.menu
)
selecta.customer_id,sum(b.multiplier) as points
fromdannys_diner.sales a
left join pointer_cte b
ona.product_id=b.product_idgroup bya.customer_id;
customer_id
points
A
860
B
940
C
360
Q10. 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 date_cte as (
select*,
DATE_ADD(join_date, INTERVAL 6 DAY) as valid_date
fromdannys_diner.members
)
selectd.customer_id,
sum(case
when b.product_id=1 then 20*b.price
when a.order_date between d.join_dateandd.valid_date then 20*b.PRICE
else 10*b.price end) as pointer
from date_cte d
left joindannys_diner.sales a
ond.customer_id=a.customer_idleft joindannys_diner.menu b
ona.product_id=b.product_idwherea.order_date<'2021-01-31'group bya.customer_id;
customer_id
pointer
A
1370
B
820
Bonus Questions
Recreate the following table output using the available data:
customer_id
order_date
product_name
price
member
A
2021-01-01
curry
15
N
A
2021-01-01
sushi
10
N
A
2021-01-07
curry
15
Y
A
2021-01-10
ramen
12
Y
A
2021-01-11
ramen
12
Y
A
2021-01-11
ramen
12
Y
B
2021-01-01
curry
15
N
B
2021-01-02
curry
15
N
B
2021-01-04
sushi
10
N
B
2021-01-11
sushi
10
Y
B
2021-01-16
ramen
12
Y
B
2021-02-01
ramen
12
Y
C
2021-01-01
ramen
12
N
C
2021-01-01
ramen
12
N
C
2021-01-07
ramen
12
N
selecta.customer_id, a.order_date, b.product_name,
b.price,
CASE
WHEN c.join_date>a.order_date THEN 'N'
WHEN c.join_date<=a.order_date THEN 'Y'
ELSE 'N' end as member
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idleft joindannys_diner.members C
ona.customer_id=c.customer_idorder bya.customer_id, a.order_date, b.product_name;
Danny also requires further information about the ranking of customer products, but he purposely does not need the ranking for non-member purchases so he expects null ranking values for the records when customers are not yet part of the loyalty program.
customer_id
order_date
product_name
price
member
ranking
A
2021-01-01
curry
15
N
null
A
2021-01-01
sushi
10
N
null
A
2021-01-07
curry
15
Y
1
A
2021-01-10
ramen
12
Y
2
A
2021-01-11
ramen
12
Y
3
A
2021-01-11
ramen
12
Y
3
B
2021-01-01
curry
15
N
null
B
2021-01-02
curry
15
N
null
B
2021-01-04
sushi
10
N
null
B
2021-01-11
sushi
10
Y
1
B
2021-01-16
ramen
12
Y
2
B
2021-02-01
ramen
12
Y
3
C
2021-01-01
ramen
12
N
null
C
2021-01-01
ramen
12
N
null
C
2021-01-07
ramen
12
N
null
with temp_cte as(selecta.customer_id, a.order_date, b.product_name,
b.price,
CASE
WHEN c.join_date>a.order_date THEN 'N'
WHEN c.join_date<=a.order_date THEN 'Y'
ELSE 'N' end as member
fromdannys_diner.sales a
left joindannys_diner.menu b
ona.product_id=b.product_idleft joindannys_diner.members C
ona.customer_id=c.customer_idorder bya.customer_id, a.order_date, b.product_name)
select*, CASE
when member ='N' then 'null'
else rank() over(PARTITION by customer_id, member order by order_date) end as ranking
from temp_cte;