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.
π Entity Relationship Diagram
β Case Study Questions
What is the total amount each customer spent at the restaurant?
How many days has each customer visited the restaurant?
What was the first item from the menu purchased by each customer?
What is the most purchased item on the menu and how many times was it purchased by all customers?
Which item was the most popular for each customer?
Which item was purchased first by the customer after they became a member?
Which item was purchased just before the customer became a member?
What is the total items and amount spent for each member before they became a member?
If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
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?
ποΈ Bonus Questions
Join All The Things - Create a table that has these columns: customer_id, order_date, product_name, price, member (Y/N).
Rank All The Things - Based on the table above, add one column: ranking.
Q1. What is the total amount each customer spent at the restaurant?
SELECTs.customer_id,
SUM(m.price) AS total_pay
FROM sales s
JOIN menu m
ONs.product_id=m.product_idGROUP BYs.customer_idORDER BYs.customer_id;
customer_id
total_pay
A
76
B
74
C
36
Q2. How many days has each customer visited the restaurant?
SELECT
customer_id,
COUNT(DISTINCT order_date) AS visit_count
FROM sales
GROUP BY customer_id;
customer_id
visit_count
A
4
B
6
C
2
Q3. What was the first item from the menu purchased by each customer?
WITH orderRank AS (
SELECT
customer_id,
product_id,
order_date,
DENSE_RANK() OVER(PARTITIONBY customer_id ORDER BY order_date) AS rnk
FROM sales
)
SELECTo.customer_id,
o.order_date,
m.product_nameFROM orderRank o
JOIN menu m
ONo.product_id=m.product_idWHEREo.rnk=1GROUP BYo.customer_id, o.order_date, m.product_name;
customer_id
order_date
product_name
A
2021-01-01
curry
A
2021-01-01
sushi
B
2021-01-01
curry
C
2021-01-01
ramen
Q4. What is the most purchased item on the menu and how many times was it purchased by all customers?
SELECT
TOP 1s.product_id,
m.product_name,
COUNT(*) AS most_purch
FROM sales s
JOIN menu m
ONs.product_id=m.product_idGROUP BYs.product_id, m.product_name;
product_id
product_name
most_purch
3
ramen
8
Q5. Which item was the most popular for each customer?
WITH freqRank AS (
SELECT
customer_id,
product_id,
COUNT(*) AS purch_freq,
DENSE_RANK() OVER(PARTITIONBY customer_id ORDER BYCOUNT(*) DESC) AS rnk
FROM sales
GROUP BY customer_id, product_id
)
SELECTf.customer_id,
m.product_name,
f.purch_freqFROM freqRank f
JOIN menu m
ONf.product_id=m.product_idWHEREf.rnk=1ORDER BYf.customer_id;
customer_id
product_name
purch_freq
A
ramen
3
B
sushi
2
B
curry
2
B
ramen
2
C
ramen
3
Q6. Which item was purchased first by the customer after they became a member?
WITH orderAfterMember AS (
SELECTs.customer_id,
mn.product_name,
s.order_date,
m.join_date,
DENSE_RANK() OVER(PARTITIONBYs.customer_idORDER BYs.order_date) AS rnk
FROM sales s
JOIN members m
ONs.customer_id=m.customer_idJOIN menu mn
ONs.product_id=mn.product_idWHEREs.order_date>=m.join_date
)
SELECT
customer_id,
product_name,
order_date,
join_date
FROM orderAfterMember
WHERE rnk =1;
customer_id
product_name
order_date
join_date
A
curry
2021-01-07
2021-01-07
B
sushi
2021-01-11
2021-01-09
Q7. Which item was purchased just before the customer became a member?
WITH orderBeforeMember AS (
SELECTs.customer_id,
mn.product_name,
s.order_date,
m.join_date,
DENSE_RANK() OVER(PARTITIONBYs.customer_idORDER BYs.order_dateDESC) AS rnk
FROM sales s
JOIN members m
ONs.customer_id=m.customer_idJOIN menu mn
ONs.product_id=mn.product_idWHEREs.order_date<m.join_date
)
SELECT
customer_id,
product_name,
order_date,
join_date
FROM orderBeforeMember
WHERE rnk =1;
customer_id
product_name
order_date
join_date
A
sushi
2021-01-01
2021-01-07
A
curry
2021-01-01
2021-01-07
B
sushi
2021-01-04
2021-01-09
Q8. What is the total items and amount spent for each member before they became a member?
SELECTs.customer_id,
COUNT(s.product_id) AS total_items,
SUM(mn.price) AS total_spend
FROM sales s
JOIN members m
ONs.customer_id=m.customer_idJOIN menu mn
ONs.product_id=mn.product_idWHEREs.order_date<m.join_dateGROUP BYs.customer_id;
customer_id
total_items
total_spend
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?
Note: Only customers who are members receive points when purchasing items
WITH CustomerPoints AS (
SELECTs.customer_id,
CASEWHENs.customer_idIN (SELECT customer_id FROM members) ANDmn.product_name='sushi'THENmn.price*20WHENs.customer_idIN (SELECT customer_id FROM members) ANDmn.product_name!='sushi'THENmn.price*10ELSE0ENDAS points
FROM menu mn
JOIN sales s
ONmn.product_id=s.product_id
)
SELECT
customer_id,
SUM(points) AS total_points
FROM CustomerPoints
GROUP BY customer_id;
customer_id
total_points
A
860
B
940
C
0
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 programDates AS (
SELECT
customer_id,
join_date,
DATEADD(d, 6, join_date) AS valid_date,
EOMONTH('2021-01-01') AS last_date
FROM members
)
SELECTp.customer_id,
SUM(CASEWHENs.order_dateBETWEENp.join_dateANDp.valid_dateTHENm.price*20WHENm.product_name='sushi'THENm.price*20ELSEm.price*10END) AS total_points
FROM sales s
JOIN programDates p
ONs.customer_id=p.customer_idJOIN menu m
ONs.product_id=m.product_idWHEREs.order_date<= last_date
GROUP BYp.customer_id;
customer_id
total_points
A
1370
B
820
Join All The Things
SELECTs.customer_id,
s.order_date,
mn.product_name,
mn.price,
CASEWHENs.order_date>=m.join_dateTHEN'Y'ELSE'N'ENDAS member
FROM sales s
JOIN menu mn
ONs.product_id=mn.product_idLEFT JOIN members m
ONs.customer_id=m.customer_id;
customer_id
order_date
product_name
price
member
A
2021-01-01
sushi
10
N
A
2021-01-01
curry
15
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
Rank All The Things
WITH customerStatus AS(
SELECTs.customer_id,
s.order_date,
mn.product_name,
mn.price,
CASEWHENs.order_date>=m.join_dateTHEN'Y'ELSE'N'ENDAS member
FROM sales s
JOIN menu mn
ONs.product_id=mn.product_idLEFT JOIN members m
ONs.customer_id=m.customer_id
)
SELECT*,
CASEWHEN member ='Y'THENDENSE_RANK() OVER(PARTITIONBY customer_id, member ORDER BY order_date)
ELSEnullENDAS ranking
FROM customerStatus;