Watch 3 video solutions for Maximum Transaction Each Day, a medium level problem involving Database. This walkthrough by Everyday Data Science has 3,955 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Table: Transactions
+----------------+----------+ | Column Name | Type | +----------------+----------+ | transaction_id | int | | day | datetime | | amount | int | +----------------+----------+ transaction_id is the column with unique values for this table. Each row contains information about one transaction.
Write a solution to report the IDs of the transactions with the maximum amount on their respective day. If in one day there are multiple such transactions, return all of them.
Return the result table ordered by transaction_id in ascending order.
The result format is in the following example.
Example 1:
Input: Transactions table: +----------------+--------------------+--------+ | transaction_id | day | amount | +----------------+--------------------+--------+ | 8 | 2021-4-3 15:57:28 | 57 | | 9 | 2021-4-28 08:47:25 | 21 | | 1 | 2021-4-29 13:28:30 | 58 | | 5 | 2021-4-28 16:39:59 | 40 | | 6 | 2021-4-29 23:39:28 | 58 | +----------------+--------------------+--------+ Output: +----------------+ | transaction_id | +----------------+ | 1 | | 5 | | 6 | | 8 | +----------------+ Explanation: "2021-4-3" --> We have one transaction with ID 8, so we add 8 to the result table. "2021-4-28" --> We have two transactions with IDs 5 and 9. The transaction with ID 5 has an amount of 40, while the transaction with ID 9 has an amount of 21. We only include the transaction with ID 5 as it has the maximum amount this day. "2021-4-29" --> We have two transactions with IDs 1 and 6. Both transactions have the same amount of 58, so we include both in the result table. We order the result table by transaction_id after collecting these IDs.
Follow up: Could you solve it without using the MAX() function?
Problem Overview: The task is to return the transaction with the highest amount for each day from a transactions table. If multiple transactions occur on the same day, only the one with the maximum amount should appear in the result.
Approach 1: GROUP BY + Join (O(n log n) time, O(n) space)
One straightforward strategy is to compute the maximum transaction amount per day using GROUP BY day. This produces a small table with day and MAX(amount). Join this result back to the original transactions table on both day and amount. The join filters rows so only transactions matching the daily maximum remain. The database engine typically sorts or hashes groups internally, leading to about O(n log n) time with O(n) intermediate storage.
This method works in almost every SQL dialect and is useful when SQL aggregation functions are preferred over analytic features. However, joins can become less readable as queries grow more complex.
Approach 2: Window Function with ROW_NUMBER() (O(n log n) time, O(n) space)
A cleaner solution uses a window function. Partition transactions by day and rank them by amount in descending order:
ROW_NUMBER() OVER (PARTITION BY day ORDER BY amount DESC)
This assigns rank 1 to the largest transaction for each day. After computing the ranking, filter rows where the rank equals 1. The database sorts rows within each partition to compute the ranking, giving O(n log n) time complexity and O(n) space for intermediate results.
Window functions are designed for exactly this type of per-group ranking problem. They avoid extra joins and keep the query compact. Most modern SQL engines including MySQL, PostgreSQL, and SQL Server support this pattern. It is a common technique in database queries and especially in window function problems.
Recommended for interviews: The window function approach using ROW_NUMBER() is the expected solution. It shows strong familiarity with analytic SQL features and keeps the query concise. The GROUP BY + join approach still demonstrates correct reasoning and works in systems without window function support, but ranking with ROW_NUMBER() is the cleaner and more scalable solution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| GROUP BY + Join | O(n log n) | O(n) | When window functions are unavailable or when using basic SQL aggregation |
| Window Function (ROW_NUMBER) | O(n log n) | O(n) | Best choice for ranking rows per group in modern SQL databases |