Professional Documents
Culture Documents
Problem Statement:-
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.
Danny has provided you with a sample of his overall customer data due to privacy issues - but he
hopes that these examples are enough for you to write fully functioning SQL queries to help him
answer his questions!
Danny has shared with you 3 key datasets for this case study:
⮚ Sales
⮚ Menu
⮚ Members
Case Study Questions:-
1. What is the total amount each customer spent at the restaurant?
2. How many days has each customer visited the restaurant?
3. What was the first item from the menu purchased by each customer?
4. What is the most purchased item on the menu and how many times was it purchased by all
customers?
5. Which item was the most popular for each customer?
6. Which item was purchased first by the customer after they became a member?
7. Which item was purchased just before the customer became a member?
8. What is the total items and amount spent for each member before they became a member?
9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would
each customer have?
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?
Solution:-
I will use Microsoft SQL Server Management studio and These are functions used
I will use DISTINCT AND COUNT FUNCTION to find out the number of customers is visited the Restaurant.
Answer:
Customer A visited 4 times.
Customer B visited 6 times.
Customer C visited 2 times.
3. What was the first item from the menu purchased by each customer?
WITH CTE_order_sales AS
(
SELECT customer_id, order_date, product_name,
DENSE_RANK() OVER(PARTITION BY s.customer_id
ORDER BY s.order_date) AS rank
FROM dbo.sales AS s
JOIN dbo.menu AS m
ON s.product_id = m.product_id
)
SELECT * FROM CTE_order_sales
After that, we use GROUP BY the columns to show rank = 1 only.
Answer:
Customer A’s first orders are 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?
Answer:-
The most purchased item on the menu is ramen.
5 . Which item was the most popular for each customer?
Again, we create a CTE to rank the number of orders for each product by DESC order for each
customer.
WITH CTE_fav_item AS
(
SELECT s.customer_id, m.product_name,
COUNT(m.product_id) AS order_count,
DENSE_RANK() OVER(PARTITION BY s.customer_id
ORDER BY COUNT(s.customer_id) DESC) AS rank
FROM dbo.menu AS m
JOIN dbo.sales AS s
ON m.product_id = s.product_id
GROUP BY s.customer_id, m.product_name
)
SELECT * FROM CTE_fav_item
Then, we generate results where the rank of product = 1 only as of the most popular product for
an individual customer.
ANSWER:-
> Customer A and C’s favorite item is ramen.
6. Which item was purchased first by the customer after they became a member?
We will Create Another CTE. In this CTE, we filter order_date to be on or after their join_date and then
rank the product_id by the order_date.
WITH CTE_member_sales AS
(
SELECT s.customer_id, m.join_date, s.order_date, s.product_id,
DENSE_RANK() OVER(PARTITION BY s.customer_id
ORDER BY s.order_date) AS rank
FROM sales AS s
JOIN members AS m
ON s.customer_id = m.customer_id
WHERE s.order_date >= m.join_date
)
SELECT * FROM CTE_member_sales
Next, we will filter the table by rank = 1 to show the first item purchased by the customer.
Answer:
After Customer A became a member, his/her first order is curry, whereas it’s sushi for Customer B.
7. Which item was purchased just before the customer became a member?
WITH CTE_prior_member_purchased AS
(
SELECT s.customer_id, m.join_date, s.order_date, s.product_id,
DENSE_RANK() OVER(PARTITION BY s.customer_id
ORDER BY s.order_date DESC) AS rank
FROM sales AS s
JOIN members AS
ON s.customer_id = m.customer_id
WHERE s.order_date < m.join_date
)
SELECT * FROM CTE_prior_member_purchased
Then, pull the table to show the last item ordered by the customer before becoming a member.
SELECT s.customer_id, s.order_date, m2.product_name
FROM CTE_prior_member_purchased AS s
JOIN menu AS m2
ON s.product_id = m2.product_id
WHERE rank = 1;
Answer:
Customer A’s order before he/she became a member is sushi and curry and Customer B’s order is
sushi. That must have been a real good sushi!
8. What is the total items and amount spent for each member before they became a member?
First, filter order_date before their join_date. Then, COUNT unique product_id and SUM the prices total
spent before becoming a member.
9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier — how
many points would each customer have?
Again, we will Create a CTE
WITH CTE_price_points AS
(
SELECT *,
CASE
WHEN product_id = 1 THEN price * 20
ELSE price * 10
END AS points
FROM menu
)
SELECT* FROM CTE_price_points
Using the table above, We SUM the price, Match it to the product_id and SUM the Total_points.
ANSWER:-
● Day -X to Day 1 (customer becomes a member (join_date), each $1 spent is 10 points and for
sushi, each $1 spent is 20 points.
● Day 1 (join_date) to Day 7 (valid_date), each $1 spent for all items is 20 points.
● Day 8 to last day of Jan 2021 (last_date), each $1 spent is 10 points and sushi 2 x points.
Answer:
● Customer A has 1,370points.
● Customer B has 820 points
Join All The Things:-
Recreate the table with: customer_id, order_date, product_name, price, member (Y/N)
Insights
From the analysis, we discover a few interesting insights that would be certainly useful for Danny .
● Customer A and C loves ramen whereas Customer B seems to enjoy sushi, curry and ramen
equally. Who knows, I might be Customer B!
● Customer A is the 1st member of Danny’s Diner and his first order is curry. Gotta fulfill his curry cravings!
● The last item ordered by Customers A and B before they became members are sushi and curry. Does it mean
both of these items are the deciding factor? It must be really delicious for them to sign up as members!
● Before they became members, both Customers A and B spent $50 and $80 .
● Throughout Jan 2021, their points for Customer A: 860, Customer B: 940 and Customer C: 360.
● Assuming that members can earn 2x a week from the day they became a member with bonus 2x
points for sushi, Customer A has 660 points and Customer B has 340 by the end of Jan 2021.