Friday Purchases I - Problem
Friday Purchases Analysis

You're a data analyst for an e-commerce company, and your manager wants to understand customer spending patterns on Fridays during November 2023. This analysis will help determine if Friday promotions are effective and identify which weeks saw the highest Friday spending.

Given a Purchases table with customer transactions, your task is to:
• Calculate the total spending on each Friday in November 2023
• Group results by week of the month
• Only include weeks that had at least one Friday purchase
• Return results ordered by week number

Table Schema:
Purchases (user_id, purchase_date, amount_spend)
user_id: Customer identifier
purchase_date: Date of purchase (November 1-30, 2023)
amount_spend: Amount spent in dollars

The goal is to identify which Fridays generated the most revenue and help the business optimize their Friday marketing campaigns.

Input & Output

example_1.sql — Basic Friday Purchases
$ Input: Purchases table: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 11 | 2023-11-03 | 120 | | 15 | 2023-11-10 | 80 | | 11 | 2023-11-10 | 55 | | 17 | 2023-11-24 | 95 |
Output: | week_of_month | total_amount | |---------------|---------------| | 1 | 120 | | 2 | 135 | | 4 | 95 |
💡 Note: Nov 3rd (Friday) is in week 1 with $120. Nov 10th (Friday) is in week 2 with $80+$55=$135. Nov 24th (Friday) is in week 4 with $95. Week 3 had no Friday purchases, so it's excluded.
example_2.sql — Multiple Users Same Friday
$ Input: Purchases table: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 12 | 2023-11-17 | 200 | | 13 | 2023-11-17 | 150 | | 14 | 2023-11-17 | 75 | | 15 | 2023-11-03 | 100 |
Output: | week_of_month | total_amount | |---------------|---------------| | 1 | 100 | | 3 | 425 |
💡 Note: Nov 3rd (Friday, week 1) has $100. Nov 17th (Friday, week 3) has multiple purchases totaling $200+$150+$75=$425.
example_3.sql — No Friday Purchases
$ Input: Purchases table: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 11 | 2023-11-01 | 120 | | 12 | 2023-11-02 | 80 | | 13 | 2023-11-04 | 200 |
Output: | week_of_month | total_amount | |---------------|---------------|
💡 Note: Nov 1st is Wednesday, Nov 2nd is Thursday, Nov 4th is Saturday. No Friday purchases exist, so the result is empty.

Constraints

  • purchase_date ranges from November 1, 2023 to November 30, 2023
  • 1 ≤ user_id ≤ 106
  • 1 ≤ amount_spend ≤ 104
  • Multiple purchases by same user on same day are allowed
  • Only include weeks with at least one Friday purchase

Visualization

Tap to expand
Friday Purchases Analysis WorkflowNovember 2023All PurchasesEvery DayMon Tue Wed Thu Fri Sat SunFilter FridaysDAYOFWEEK() = 6Nov 3, 10, 17, 24✓ Only FridaysCalculate WeeksCEILING(DAY/7)Nov 3→Week 1Nov 10→Week 2Nov 17→Week 3, etc.GROUP BY WeekSUM(amount_spend)Week 1: $450Week 2: $320Week 4: $180Final ResultsORDER BY weekOnly weeks withFriday purchasesKey Insight: Single Query Efficiency✓ One table scan ✓ Built-in date functions ✓ Database optimized GROUP BY ✓ Efficient aggregationTime: O(n) | Space: O(weeks) | Much faster than multiple scans
Understanding the Visualization
1
Identify Fridays
Use DAYOFWEEK() function to filter only Friday transactions from the purchases table
2
Calculate Weeks
Determine which week of November each Friday belongs to using date arithmetic
3
Group & Sum
Aggregate spending by week using GROUP BY and SUM functions
4
Filter & Sort
Exclude weeks with no Friday purchases and order results by week number
Key Takeaway
🎯 Key Insight: Using SQL date functions like `DAYOFWEEK()` and mathematical expressions for week calculation allows processing all data in a single efficient query with optimal database engine optimization.
Asked in
Amazon 45 Meta 38 Google 32 Microsoft 28
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen