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, CONCAT('Rs.', SUM(price)) AS total_sum
FROMdannys_diner.salesINNER JOINdannys_diner.menuONsales.product_id=menu.product_idGROUP BY customer_id
ORDER BY customer_id;
Result
customer_id
total_sum
A
Rs.76
B
Rs.74
C
Rs.36
2. How many days has each customer visited the restaurant?
SQL Query:
SELECT customer_id, COUNT(DISTINCT order_date) AS visits
FROMdannys_diner.salesGROUP BY customer_id;
Result
customer_id
visits
A
4
B
6
C
2
3. What was the first item from the menu purchased by each customer?
SQL Query:
WITH cte AS
(SELECT customer_id,
order_date,
product_name,
DENSE_RANK() OVER(PARTITION BY s.customer_idORDER BYs.order_date) AS rank_num
FROMdannys_diner.salesAS s
JOINdannys_diner.menuAS m ONs.product_id=m.product_id)
SELECT customer_id,
product_name
FROM cte
WHERE rank_num =1GROUP BY customer_id,
product_name;
Result
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?
SQL Query:
WITH q_4 AS
(SELECT*FROMdannys_diner.salesINNER JOINdannys_diner.menuONsales.product_id=menu.product_id)
SELECT product_name, COUNT(product_name) AS total_purchases
FROM q_4
GROUP BY product_name
ORDER BY total_purchases DESCLIMIT1;
Result
product_name
total_purchases
ramen
8
5. Which item was the most popular for each customer?
SQL Query:
SELECT customer_id, product_name
FROM (
SELECTsales.customer_id,
menu.product_name,
COUNT(menu.product_name) AS product_count,
RANK() OVER (PARTITION BY sales.customer_idORDER BYCOUNT(menu.product_name) DESC) AS rank
FROMdannys_diner.salesINNER JOINdannys_diner.menuONsales.product_id=menu.product_idGROUP BYsales.customer_id, menu.product_name
) ranked
WHERE rank =1;
Result
customer_id
product_name
A
ramen
B
ramen
B
curry
B
sushi
C
ramen
6. Which item was purchased first by the customer after they became a member?
SQL Query:
SELECTcte.customer_id, cte.product_name, cte.order_dateFROM (
SELECTs.customer_id, m.product_name, s.order_date,
ROW_NUMBER() OVER(PARTITION BY s.customer_idORDER BYs.order_date) AS row_num
FROMdannys_diner.salesAS s
LEFT JOINdannys_diner.menuAS m ONs.product_id=m.product_idLEFT JOINdannys_diner.membersAS mb ONs.customer_id=mb.customer_idWHEREs.order_date>mb.join_date
) cte
WHEREcte.row_num=1ORDER BYcte.order_date;
Result
customer_id
product_name
order_date
A
ramen
2021-01-10T00:00:00.000Z
B
sushi
2021-01-11T00:00:00.000Z
7. Which item was purchased just before the customer became a member?
SQL Query:
SELECT customer_id, product_name
FROM (
SELECTs.customer_id, m.product_name, s.order_date,
RANK() OVER(PARTITION BY s.customer_idORDER BYs.order_dateDESC) AS row_num
FROMdannys_diner.salesAS s
LEFT JOINdannys_diner.menuAS m ONs.product_id=m.product_idLEFT JOINdannys_diner.membersAS mb ONs.customer_id=mb.customer_idWHEREs.order_date<mb.join_date
) cte
WHERE row_num =1ORDER BY customer_id;
Result
customer_id
product_name
A
sushi
A
curry
B
sushi
8. What is the total items and amount spent for each member before they became a member?
SQL Query:
WITH cte AS (
SELECT s.*, m.product_name, m.price,
CASE
WHEN mb.join_date<=s.order_date THEN 'Y'
WHEN mb.join_date>s.order_date THEN 'N'
ELSE 'N'
END AS join_status
FROMdannys_diner.salesAS s
LEFT JOINdannys_diner.menuAS m ONs.product_id=m.product_idLEFT JOINdannys_diner.membersAS mb ONs.customer_id=mb.customer_id
)
SELECTcte.customer_id, COUNT(cte.product_id) AS product_count, SUM(cte.price) AS total_price
FROM cte
WHEREcte.join_status='N'GROUP BYcte.customer_idORDER BYcte.customer_id;
Result
customer_id
product_count
total_price
A
2
25
B
3
40
C
3
36
9. If each $1 spent equates to 10 points and sushi has a 2x points multiplier - how many points would each customer have?
SQL Query:
WITH CTE AS (
SELECT s.*,
CASE
WHEN m.product_name='sushi' THEN (m.price*10) *2
WHEN m.product_name='curry' THEN m.price*10
WHEN m.product_name='ramen' THEN m.price*10
END AS points
FROMdannys_diner.salesAS s
LEFT JOINdannys_diner.menuAS m ONs.product_id=m.product_id
)
SELECT customer_id, SUM(points) AS sum_of_points
FROM CTE
GROUP BY customer_id;
Result
customer_id
sum_of_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?
SQL Query:
WITH cte AS (
SELECTa.customer_id,
CASE
WHEN c.product_name='sushi' THEN 2*c.price
WHEN a.order_date BETWEEN b.join_dateANDb.join_date+ INTERVAL '6 DAY' THEN 2*c.price
ELSE c.price
END AS newprice
FROMdannys_diner.sales a
JOINdannys_diner.menu c ONa.product_id=c.product_idJOINdannys_diner.members b ONa.customer_id=b.customer_idWHEREa.order_date<='2021-01-31'
)
SELECT customer_id, SUM(newprice) *10AS total_points
FROM cte
GROUP BY customer_id
HAVING customer_id IN ('A', 'B');