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?
select customer_id, sum(price) as total_sales
fromdannys_diner.salesas s
joindannys_diner.menuas m
ons.product_id=m.product_idgroup by customer_id
order by customer_id;
Answer:
customer_id
total_sales
A
76
B
74
C
36
2. How many days has each customer visited the restaurant?
select customer_id, count(DISTINCT order_date)
fromdannys_diner.salesgroup by customer_id;
Answer:
customer_id
total_sales
A
4
B
6
C
2
3. What was the first item from the menu purchased by each customer?
WITH cte_customers AS
(
SELECT customer_id, order_date, product_name,
DENSE_RANK() OVER(PARTITION BY s.customer_idORDER BYs.order_date) AS rank
FROMdannys_diner.salesAS s
JOINdannys_diner.menuAS m
ONs.product_id=m.product_id
)
SELECT customer_id, product_name
FROM cte_customers
WHERE rank =1GROUP BY customer_id, product_name;
Answer:
customer_id
product_name
A
curry
A
sushi
B
curry
C
ramen
4. What is the most purchased item on the menu and how many times was it purchased by all customers?
-- most purchased item on the menuselect Distinctm.product_name, count(s.order_date) as most_purchased
fromdannys_diner.menuas m
joindannys_diner.salesas s
onm.product_id=s.product_idgroup bym.product_id, product_name
order by most_purchased desclimit1;
Answer:
product_name
most_purchased
ramen
8
-- Most popular item is ramen.
-- how many times was "ramen" purchased by all customers?Selectss.customer_id, mm.product_name, Count(ss.product_id) as order_count
fromdannys_diner.salesas ss
joindannys_diner.menuas mm
onss.product_id=mm.product_idwheremm.product_namelike'%ramen%'group byss.customer_id, mm.product_name;
Answer:
customer_id
product_name
order_count
A
ramen
3
A
ramen
2
B
ramen
3
5. Which item was the most popular for each customer?
with cte_popular_item as
(
Selects.customer_id, m.product_name, Count(s.product_id) as order_count,
DENSE_RANK() over(partition by s.customer_idORDER BYCOUNT(s.customer_id) DESC) as rank
fromdannys_diner.salesas s
joindannys_diner.menuas m
ons.product_id=m.product_idGROUP BYs.customer_id, m.product_name
)
SELECT customer_id, product_name, order_count
FROM cte_popular_item
WHERE rank =1;
Answer:
customer_id
product_name
order_count
A
ramen
3
B
sushi
2
B
curry
2
B
ramen
2
C
ramen
3
6. Which item was purchased first by the customer after they became a member?
with cte_first_order_after_join as
(
selects.customer_id, s.product_id, s.order_date,
dense_rank() over(partition by s.customer_idorder by(s.order_date) ) as rank
fromdannys_diner.salesas s
joindannys_diner.membersas m
ons.customer_id=m.customer_idwheres.order_date>=m.join_date
)
Selectc.customer_id, mm.product_name, c.order_datefrom cte_first_order_after_join as c
joindannys_diner.menuas mm
onc.product_id=mm.product_idwhere rank=1;
Answer:
customer_id
product_name
order_date
B
sushi
2021-01-11
A
curry
2021-01-07
7. Which item was purchased just before the customer became a member?
with cte_last_order_before_join as
(
selects.customer_id, s.product_id, s.order_date,
dense_rank() over(partition by s.customer_idorder by(s.order_date) desc) as rank
fromdannys_diner.salesas s
joindannys_diner.membersas m
ons.customer_id=m.customer_idwheres.order_date<m.join_date
)
Selectc.customer_id, mm.product_name, c.order_datefrom cte_last_order_before_join as c
joindannys_diner.menuas mm
onc.product_id=mm.product_idwhere rank=1;
Answer:
customer_id
product_name
order_date
B
sushi
2021-01-04
A
sushi
2021-01-01
A
curry
2021-01-01
8. What is the total items and amount spent for each member before they became a member?
selects.customer_id, count(distinct s.product_id) as total_item, sum(m.price) as total_amount
fromdannys_diner.salesas s
joindannys_diner.menuas m
ons.product_id=m.product_idjoindannys_diner.membersas mm
ons.customer_id=mm.customer_idwhere order_date<join_date
group bys.customer_id;
Answer:
customer_id
total_item
total_amount
A
2
25
B
2
40
9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
with cte_points as
(
Select*,
CASE WHEN product_id=1 then price*20
Else price*10
end as price_ponts
fromdannys_diner.menu
)
selects.customer_id, sum(c.price_ponts) as total_points
fromdannys_diner.salesas s
join cte_points as c
ons.product_id=c.product_idgroup bys.customer_idorder bys.customer_id;
Answer:
customer_id
total_points
A
860
B
940
C
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 dates_cte AS (
SELECT m.*,
join_date + INTERVAL '6 DAY'AS valid_date,
DATE_TRUNC('month', '2023-01-31'::DATE) + INTERVAL '1 MONTH - 1 DAY'AS last_date
FROMdannys_diner.members m
)
SELECTd.customer_id,
SUM(CASE
WHEN m.product_name='sushi' THEN 2*10*m.price
WHEN s.order_date BETWEEN d.join_dateANDd.valid_date THEN 2*10*m.price
ELSE 10*m.price
END) AS points
FROM dates_cte d
JOINdannys_diner.sales s ONd.customer_id=s.customer_idJOINdannys_diner.menu m ONs.product_id=m.product_idWHEREs.order_date<d.last_dateGROUP BYd.customer_id;
Answer:
customer_id
points
A
1370
B
820
Bonus Questions
Join All The Things(Recreating the following table)
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
Answer:
selects.customer_id, s.order_date, m.product_name,m.price,
case when s.order_date>=mm.join_date then 'Y'
else 'N'
end as membership
fromdannys_diner.salesas s
joindannys_diner.menuas m
ons.product_id=m.product_idjoindannys_diner.membersas mm
ons.customer_id=mm.customer_id;
Rank All The Things - 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
Answer:
with membership_data as(
selects.customer_id, s.order_date, m.product_name,m.price,
case when s.order_date>=mm.join_date then 'Y'
else 'N'
end as membership
fromdannys_diner.salesas s
joindannys_diner.menuas m
ons.product_id=m.product_idjoindannys_diner.membersas mm
ons.customer_id=mm.customer_id
)
select*,
case when membership='N' then NULL
else Rank() over(PARTITION BY customer_id, membership ORDER BY order_date)
END AS Ranking
from membership_data;