Friday Purchases II - Problem
Friday Purchases II - Master SQL Window Functions & Date Analysis

You're analyzing shopping patterns for Black Friday preparation! Given a Purchases table containing user transactions throughout November 2023, your mission is to calculate total spending on each Friday of every week.

Table Schema:
Purchases
+---------------+------+
| Column Name | Type |
+---------------+------+
| user_id | int |
| purchase_date | date |
| amount_spend | int |
+---------------+------+

Key Requirements:
• Focus only on Fridays in November 2023
• Calculate total spending per Friday by week
• If no purchases occurred on a Friday, show 0
• Return results ordered by week of month (ascending)

This problem tests your ability to work with date functions, conditional aggregation, and generating missing data points - essential skills for real-world analytics!

Input & Output

example_1.sql — Basic Friday Spending
$ Input: Purchases: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 11 | 2023-11-03 | 200 | | 15 | 2023-11-10 | 500 | | 11 | 2023-11-03 | 300 | | 17 | 2023-11-17 | 100 |
Output: | week_of_month | total_amount | |---------------|---------------| | 1 | 500 | | 2 | 500 | | 3 | 100 | | 4 | 0 |
💡 Note: Week 1 (Nov 3): Users 11 spent $200 + $300 = $500. Week 2 (Nov 10): User 15 spent $500. Week 3 (Nov 17): User 17 spent $100. Week 4 (Nov 24): No purchases, so $0.
example_2.sql — Missing Friday Weeks
$ Input: Purchases: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 12 | 2023-11-01 | 400 | | 13 | 2023-11-03 | 150 | | 14 | 2023-11-24 | 250 |
Output: | week_of_month | total_amount | |---------------|---------------| | 1 | 150 | | 2 | 0 | | 3 | 0 | | 4 | 250 |
💡 Note: Only Nov 3 (Friday, Week 1) and Nov 24 (Friday, Week 4) had purchases. Nov 1 was a Wednesday, so it's filtered out. Weeks 2 and 3 had no Friday purchases.
example_3.sql — Edge Case: All Empty Fridays
$ Input: Purchases: | user_id | purchase_date | amount_spend | |---------|---------------|---------------| | 20 | 2023-11-01 | 300 | | 21 | 2023-11-02 | 400 | | 22 | 2023-11-06 | 500 |
Output: | week_of_month | total_amount | |---------------|---------------| | 1 | 0 | | 2 | 0 | | 3 | 0 | | 4 | 0 |
💡 Note: None of the purchase dates (Nov 1, 2, 6) fall on Fridays. All Friday weeks show $0 spending, demonstrating the importance of handling missing data.

Constraints

  • purchase_date ranges from November 1, 2023 to November 30, 2023 inclusive
  • 1 ≤ user_id ≤ 103
  • 1 ≤ amount_spend ≤ 106
  • The table may contain multiple purchases by the same user on the same date
  • Results must be ordered by week_of_month in ascending order

Visualization

Tap to expand
Friday Calendar Generation StrategyNovember 2023 - Friday FocusStep 1: Generate Friday CalendarWeek 1Nov 3 (Fri)Week 2Nov 10 (Fri)Week 3Nov 17 (Fri)Week 4Nov 24 (Fri)Step 2: Aggregate Purchase DataNov 3$500 totalNov 10No dataNov 17$300 totalNov 24No dataStep 3: LEFT JOIN + COALESCE ResultWeek 1$500Week 2$0Week 3$300Week 4$0🎯 Key Insight: Generate complete Friday calendar first, then LEFT JOIN ensures no missing weeks!
Understanding the Visualization
1
Create Friday Calendar
Generate all 4 Friday dates in November 2023
2
Calculate Week Numbers
Use mathematical formula to determine week positions
3
Aggregate Real Data
Sum all Friday purchases by date
4
Join & Fill Gaps
LEFT JOIN calendar with data, COALESCE NULLs to 0
Key Takeaway
🎯 Key Insight: Generate all Friday dates upfront using CTEs, then LEFT JOIN with purchase data to guarantee complete week coverage with automatic zero-filling for missing data.
Asked in
Amazon 45 Meta 38 Google 32 Microsoft 28
34.4K Views
High Frequency
~25 min Avg. Time
1.5K 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