Maximum Transaction Each Day - Problem
You're working with a financial database that tracks daily transactions. Your task is to identify the highest value transactions for each day.
Given a table Transactions with columns:
transaction_id(int) - unique identifier for each transactionday(datetime) - the date when the transaction occurredamount(int) - the transaction amount
Write a SQL query to find the transaction IDs of all transactions that have the maximum amount on their respective day. If multiple transactions share the highest amount on the same day, include all of them.
Return results ordered by transaction_id in ascending order.
Example: If Day 1 has transactions of $100, $200, $200, then both $200 transactions should be returned.
Input & Output
example_1.sql โ Basic Example
$
Input:
Transactions table:
+----------------+------------+--------+
| transaction_id | day | amount |
+----------------+------------+--------+
| 1 | 2021-12-01 | 800 |
| 2 | 2021-12-01 | 600 |
| 3 | 2021-12-01 | 600 |
| 4 | 2021-12-02 | 900 |
| 5 | 2021-12-02 | 900 |
+----------------+------------+--------+
โบ
Output:
+----------------+
| transaction_id |
+----------------+
| 1 |
| 4 |
| 5 |
+----------------+
๐ก Note:
On 2021-12-01, transaction 1 has the maximum amount of 800. On 2021-12-02, both transactions 4 and 5 have the maximum amount of 900, so both are included.
example_2.sql โ Multiple Ties
$
Input:
Transactions table:
+----------------+------------+--------+
| transaction_id | day | amount |
+----------------+------------+--------+
| 6 | 2021-12-03 | 1000 |
| 7 | 2021-12-03 | 1000 |
| 8 | 2021-12-03 | 1000 |
| 9 | 2021-12-04 | 500 |
+----------------+------------+--------+
โบ
Output:
+----------------+
| transaction_id |
+----------------+
| 6 |
| 7 |
| 8 |
| 9 |
+----------------+
๐ก Note:
On 2021-12-03, all three transactions have the same maximum amount of 1000. On 2021-12-04, transaction 9 is the only one, so it's automatically the maximum.
example_3.sql โ Single Transaction Per Day
$
Input:
Transactions table:
+----------------+------------+--------+
| transaction_id | day | amount |
+----------------+------------+--------+
| 10 | 2021-12-05 | 250 |
| 11 | 2021-12-06 | 750 |
| 12 | 2021-12-07 | 300 |
+----------------+------------+--------+
โบ
Output:
+----------------+
| transaction_id |
+----------------+
| 10 |
| 11 |
| 12 |
+----------------+
๐ก Note:
Each day has only one transaction, so each transaction is automatically the maximum for its respective day.
Constraints
- 1 โค Number of transactions โค 105
- 1 โค transaction_id โค 106
- All transaction_id values are unique
- amount can be any positive integer
- day is in YYYY-MM-DD HH:MM:SS format
Visualization
Tap to expand
Understanding the Visualization
1
Group by Days
Separate transactions into daily buckets, like organizing separate competitions for each day
2
Rank Within Days
Within each day, rank transactions by amount from highest to lowest, with ties receiving the same rank
3
Select Champions
Pick all transactions with rank = 1 (the daily champions), handling multiple winners gracefully
4
Sort Results
Arrange the final list of champions by their transaction ID for consistent output
Key Takeaway
๐ฏ Key Insight: Window functions like RANK() allow us to partition data and perform rankings within each partition efficiently, making it perfect for finding maximum values per group while handling ties gracefully.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code