The date filtering part is what tripped me up a bit.
First, determine the most recent 3-month window by finding the maximum date in the sales table and filtering to the 3 months prior. Then aggregate total units sold per city and book within that window, and use a window function like ROW_NUMBER() or RANK() partitioned by city and ordered by total units descending to assign ranks. Finally, filter to ranks 1-3 and output the required columns.
Pro tip: Clarify whether 'most recent 3-month window' means the last 3 calendar months from the max date or the last 3 full months; also mention that ties in sales should be handled consistently (e.g., using RANK() to include ties or ROW_NUMBER() to force unique ranks).
Find the maximum date in the sales data and define the window as the 3 months leading up to that date (e.g., using DATE_SUB or INTERVAL). Filter the sales records to only include transactions within that window.
Group the filtered data by city and book identifier, summing the units sold to get total sales for each combination.
Use a window function such as ROW_NUMBER() or RANK() over a partition by city, ordered by total units sold descending, to assign a rank to each book.
Apply a filter to keep only rows where the rank is less than or equal to 3.
Return the city, book identifier, total units sold, and rank columns as specified, ensuring the final result is ordered appropriately (e.g., by city and rank).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.