8 Weeks SQL Challenge. by Omojuwa Oluwaseun by Omojuwa Oluwaseun May, 2023 Medium
8 Weeks SQL Challenge. by Omojuwa Oluwaseun by Omojuwa Oluwaseun May, 2023 Medium
Search Medium
Listen Share
1 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Introduction
I recently decided to challenge myself and participate in the 8-week sql challenge
hosted by Danny Ma. For the first case we were tasked with some business questions
that could help provide insights into the restaurant business that will help make
recommendations needed to grow
2 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
embark upon a risky venture and opens up a cute little restaurant that sells his 3
favourite foods: sushi, curry and ramen. His Diner is in need of assistance to help
You are signed out. Sign in with your member account
the restaurant(sh__@g__.com)
stay afloat —tothe restaurant
view has captured
other member-only some
stories. Sign in very basic data from
their few months of operation but have no idea how to use their data to help them
run the business.
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 personalized experience for his
loyal customers.
He plans on using these insights to help him decide whether he should expand the
existing customer loyalty program — additionally he needs help to generate some
basic datasets so his team can easily inspect the data without needing to use SQL.
Danny has provided you with a sample of his overall customer data due to privacy
issues and has shared 3 key datasets for this case study: Sales, Menu, Members
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?
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
3 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
a member?
9. If each $1 You are signed out. Sign in with your member account
spent equates to 10 points and sushi has a 2x points multiplier — how
(sh__@g__.com) to view other member-only stories. Sign in
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?
Solutions
1. To solve this question, I joined the sales and menu table, the customer id was
selected and the aggregate of the amount they spent was also selected using the
sum function.
Answer: Customer A spent $76, Customer B spent $74 while Customer C spent $36
2. To do this, I selected the customer id, and the count of their unique order dates,
the distinct function here is to enable us choose the unique number of times they
visited the restaurant
4 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
3. To solve this, CTE was created to rank each customer’s order, so that older dates
had rank 1 and earlier dates had higher ranks. After which I joined the CTE to the
menu table, using product_id , and filtered the result to return records with only 1
as the rank.
5 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Answer: Customer A’s first item ordered were Sushi & Curry, customer B’s first item
ordered was Curry, while customer C’s first item ordered was Ramen.
4. To do this, I used the count function and also aggregated it by product name to
allow us see how many times each product were bought. The limit function was set
to 1 to return only the item with the highest purchase
6 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Answer: the product with the highest purchase by all customers is Ramen and it was
purchased 8 times.
5. To know the most popular items, we need to have an idea of how many times a
customer purchased an item. to do this, a CTE was created with the customer-id,
product_name and count of each product, the Rank windows function was also used
to rank the count of products from highest to lowest. I also filtered it to return result
with ranks that only have 1
7 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Answer: Ramen was the most popular item for customer A with 1 count. Sushi,
curry, and Ramen had equal number of times they were purchased for customer B
with 2 each. Ramen was the most popular item for customer C with 3 counts.
7. To answer this question, I used a Common Table Expression (CTE) to join the
sales and menu tables. I then used the MAX function to extract the latest order date
for each customer. After which I filtered the results to only include orders made by
each customer just before they joined the member's loyalty program.
8 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Answer: Customer A ordered Sushi and Curry just before they joined the
membership, while Customer B ordered Sushi
8. To address this question, I performed a join on the three tables and utilized the
COUNT function to determine the number of times each customer made a
purchase, as well as the SUM function to calculate the total cost of each customer’s
purchases. After which I applied a WHERE clause to limit the results to purchases
made before the customer joined the loyalty program.
9 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Answer: Customer A bought a total of two items where they spent $25 while
Customer B bought a total of three items and spent $40 before they joined.
9. To solve this challenging question, I utilized the CASE statement to categorize and
group the points earned by customers based on their purchases of sushi and other
items. This statement was used to create a new column called ‘points’ that showed
the total points earned by each customer, based on the conditions set in the CASE
statement. Additionally, I joined the sales and menu tables using the common
primary key (product_id) to extract the products purchased, the amount spent on
each product, and the total points earned by each customer.
10 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Follow
I am a Data enthusiast willing to collaborate and work on projects. I work with various tools like Excel, MySql
and PowerBi
Answer: Customer A had a total point of 860, Customer B had a total point of 940,
while customer C had a total point of 360.
Bonus Question:
The following questions are related creating basic data tables that Danny and his
team can use to quickly derive insights without needing to join the underlying tables
using SQL.
Omojuwa Oluwaseun
(Y/N)
Introduction
26
11 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Omojuwa Oluwaseun
• Customer A and C loves ramen whereas Customer B seems to enjoy sushi, curry
and ramen equally.
• Customer A is the 1st member of Danny’s Diner and his first order is curry.
• Throughout Jan 2021, Customer A, Customer B and Customer C had 860 points,
12 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Feel free to share your opinion about my analysis in the comments. Suggestions on
how to optimize my SQL code for performance are also welcome.
17.9K 325
13 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Scott H. Young
1K 18
Lists
Staff Picks
300 stories · 59 saves
Self-Improvement 101
20 stories · 24 saves
Productivity 101
20 stories · 33 saves
14 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Christina Sa in UX Planet
3.8K 47
15 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
Alex Mathers
10.8K 196
132 6
16 of 17 5/13/2023, 8:35 AM
8 Weeks Sql Challenge. By: Omojuwa Oluwaseun | by Omojuwa Oluw... https://medium.com/@omojuwa_oluwaseun/8-weeks-sql-challenge-7cd...
121 2
17 of 17 5/13/2023, 8:35 AM