You are on page 1of 11

PROJECT NAME – Dinny_dinner

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

 Aggregate functions — COUNT,SUM, MIN, MAX


 Numerical functions — TOP
 Joins — Inner join, left join, Right join
 Temp tables (CTE)
 Windows function

1. What is the total amount each customer spent at the restaurant?

SELECT s.customer_id, SUM(price) AS total_Amount


FROM dbo.sales AS s
JOIN dbo.menu AS m
ON s.product_id = m.product_id
GROUP BY s.customer_id;
Answer:
> Customer A spent $76.
>Customer B spent $74.
>Customer C spent $36.

2. How many days has each customer visited the restaurant?

I will use DISTINCT AND COUNT FUNCTION to find out the number of customers is visited the Restaurant.

SELECT customer_id, COUNT(DISTINCT(order_date)) AS visit_count


FROM dbo.sales
GROUP BY customer_id;

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.

SELECT customer_id, product_name


FROM CTE_order_sales
WHERE rank = 1
GROUP BY customer_id, product_name;

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?

SELECT TOP 1 (COUNT(s.product_id)) AS most_purchased, product_name


FROM dbo.sales AS s
JOIN dbo.menu AS m
ON s.product_id = m.product_id
GROUP BY s.product_id, product_name
ORDER BY most_purchased DESC;

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.

> Customer B enjoys all items on the menu.

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.

SELECT s.customer_id, s.order_date, m2.product_name


FROM CTE_member_sales AS s
JOIN menu AS m2
ON s.product_id = m2.product_id
WHERE rank = 1

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?

Again. We will create CTE

 Create a new column rank by partitioning customer_id by DESC order_date to find


out the order_date just before the customer became a member.

 Filter order_date before join_date.

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.

SELECT s.customer_id, COUNT(DISTINCT s.product_id) AS unique_menu_item,


SUM(mm.price) AS total_sales
FROM dbo.sales AS s
JOIN dbo.members AS m
ON s.customer_id = m.customer_id
JOIN dbo.menu AS mm
ON s.product_id = mm.product_id
WHERE s.order_date < m.join_date
GROUP BY s.customer_id;

ANSWER:-Before becoming members:


 Customer A spent $ 50 on 2 items.
 Customer B spent $ 80 on 2 items

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.

SELECT s.customer_id, SUM(p.points) AS total_points


FROM CTE_price_points AS p
JOIN sales AS s
ON p.product_id = s.product_id
GROUP BY s.customer_id

ANSWER:-

Total points From A, B AND C are 860, 940 and 360.


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 CTE_dates AS
(
SELECT *,
DATEADD(DAY, 6, join_date) AS valid_date,
EOMONTH('2021-01-31') AS last_date
FROM members AS m
)
SELECT * FROM CTE_dates
Then, use CASE WHEN to allocate points by dates and product_name.

SELECT d.customer_id, s.order_date, d.join_date, d.valid_date, d.last_date,


m.product_name, m.price,
SUM(CASE
WHEN m.product_name = 'sushi' THEN 2 * 10 * m.price
WHEN s.order_date BETWEEN d.join_date AND d.valid_date THEN 2 * 10 * m.price
ELSE 10 * m.price END) AS points
FROM dates_cte AS d
JOIN sales AS s
ON d.customer_id = s.customer_id
JOIN menu AS m
ON s.product_id = m.product_id
WHERE s.order_date < d.last_date
GROUP BY d.customer_id, s.order_date, d.join_date, d.valid_date, d.last_date,
m.product_name, m.price

● 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)

SELECT s.customer_id, s.order_date, m.product_name, m.price


CASE
WHEN mm.join_date > s.order_date THEN 'N'
WHEN mm.join_date <= s.order_date THEN 'Y'
ELSE 'N'
END AS member
FROM sales AS s
LEFT JOIN menu AS m
ON s.product_id = m.product_id
LEFT JOIN members AS mm
ON s.customer_id = mm.customer_id;

Insights
From the analysis, we discover a few interesting insights that would be certainly useful for Danny .

● Customer B is the most frequent visitor with 6 visits in Jan 2021.


● Danny’s Diner’s most popular item is ramen, followed by curry and sushi.

● 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.

You might also like