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 transaction
  • day (datetime) - the date when the transaction occurred
  • amount (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
Daily Transaction Champions ๐Ÿ†Day 1Transactions๐Ÿฅ‡ ID: 2 - $200๐Ÿฅ‡ ID: 3 - $200๐Ÿฅˆ ID: 7 - $150๐Ÿฅ‰ ID: 1 - $100Champions: 2, 3Day 2Transactions๐Ÿฅ‡ ID: 5 - $300๐Ÿฅˆ ID: 4 - $150๐Ÿฅ‰ ID: 6 - $100Champion: 5Day 3Transactions๐Ÿฅ‡ ID: 8 - $400๐Ÿฅˆ ID: 9 - $350Champion: 8Final ChampionsAll Rank #1 Transactions[2, 3, 5, 8]Sorted by transaction_id๐Ÿš€ Window functions make this efficient and elegant!
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.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28 Apple 22
38.2K Views
High Frequency
~15 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