Table: Purchases
+---------------+------+ | Column Name | Type | +---------------+------+ | user_id | int | | purchase_date | date | | amount_spend | int | +---------------+------+ (user_id, purchase_date, amount_spend) is the primary key (combination of columns with unique values) for this table. purchase_date will range from November 1, 2023, to November 30, 2023, inclusive of both dates. Each row contains user id, purchase date, and amount spend.
Write a solution to calculate the total spending by users on each Friday of every week in November 2023. Output only weeks that include at least one purchase on a Friday.
Return the result table ordered by week of month in ascending order.
The result format is in the following example.
Example 1:
Input: Purchases table: +---------+---------------+--------------+ | user_id | purchase_date | amount_spend | +---------+---------------+--------------+ | 11 | 2023-11-07 | 1126 | | 15 | 2023-11-30 | 7473 | | 17 | 2023-11-14 | 2414 | | 12 | 2023-11-24 | 9692 | | 8 | 2023-11-03 | 5117 | | 1 | 2023-11-16 | 5241 | | 10 | 2023-11-12 | 8266 | | 13 | 2023-11-24 | 12000 | +---------+---------------+--------------+ Output: +---------------+---------------+--------------+ | week_of_month | purchase_date | total_amount | +---------------+---------------+--------------+ | 1 | 2023-11-03 | 5117 | | 4 | 2023-11-24 | 21692 | +---------------+---------------+--------------+ Explanation: - During the first week of November 2023, transactions amounting to $5,117 occurred on Friday, 2023-11-03. - For the second week of November 2023, there were no transactions on Friday, 2023-11-10. - Similarly, during the third week of November 2023, there were no transactions on Friday, 2023-11-17. - In the fourth week of November 2023, two transactions took place on Friday, 2023-11-24, amounting to $12,000 and $9,692 respectively, summing up to a total of $21,692. Output table is ordered by week_of_month in ascending order.
Problem Overview: The database contains a Purchases table with purchase dates and amounts. You need to report the total purchase amount for every Friday in November 2023. The key tasks are filtering rows that fall on a Friday, restricting results to the November 2023 range, and aggregating the purchase totals for each date.
Approach 1: Filter by Friday Using Date Functions (O(n) time, O(1) space)
The direct solution uses MySQL date functions to detect Fridays and then aggregates purchase amounts. Functions like WEEKDAY() or DAYOFWEEK() allow you to determine the day of the week for each purchase_date. Filter rows where the weekday corresponds to Friday and restrict the range to November 2023 using MONTH(), YEAR(), or a date range filter such as BETWEEN '2023-11-01' AND '2023-11-30'. After filtering, group by the purchase date and compute the total with SUM(amount). This approach scans the table once, making it O(n) time where n is the number of purchase rows. Memory usage stays O(1) because aggregation happens within the query engine without additional structures.
This technique relies on SQL database operations like filtering and grouping. It is concise and performs well for large datasets because the database optimizer can use indexes on the date column if available.
Approach 2: Generate Fridays and Join Purchases (O(n + f) time, O(f) space)
If the requirement is to show every Friday in the month even when no purchases occurred, you first generate the list of Fridays in November 2023. This can be done using MySQL date arithmetic such as DATE_ADD() or a recursive sequence to produce all dates, then filtering to Fridays with WEEKDAY(date) = 4. Once the Friday list is generated, perform a LEFT JOIN with the Purchases table on the date field and aggregate using SUM(amount). Missing purchase rows produce NULL, which can be converted to 0 using COALESCE().
This pattern combines calendar generation with aggregation and is common in reporting queries. The complexity becomes O(n + f), where n is the number of purchase rows and f is the number of generated Friday dates (a very small constant for a single month). The approach demonstrates strong understanding of SQL SQL date handling and reporting logic.
Recommended for interviews: The date-function filtering approach is what interviewers typically expect. It shows you understand SQL aggregation, date filtering, and weekday extraction. If the problem requires displaying Fridays without purchases, generating a calendar table and using a LEFT JOIN demonstrates deeper knowledge of reporting queries and database design patterns.
The date functions we use include:
DATE_FORMAT(date, format): Formats a date as a stringDAYOFWEEK(date): Returns the day of the week for a date, where 1 represents Sunday, 2 represents Monday, and so onDAYOFMONTH(date): Returns the day of the month for a dateFirst, we use the DATE_FORMAT function to format the date in the form of YYYYMM, then filter out the records of November 2023 that fall on a Friday. Next, we group the records by purchase_date and calculate the total consumption amount for each Friday.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Filter by Friday with Date Functions | O(n) | O(1) | Best for direct aggregation when only existing Friday purchases need to be reported |
| Generate Fridays and LEFT JOIN Purchases | O(n + f) | O(f) | Use when every Friday must appear in the result, even if no purchases exist |
Leetcode MEDIUM 2993 - Friday Purchases I - Solved by Everyday Data Science | DAYNAME(), WEEK() • Everyday Data Science • 764 views views
Practice Friday Purchases I with our built-in code editor and test cases.
Practice on FleetCode