For each banned user, return user_id and total_amount: the sum of their transactions with purchase_date strictly before ban_date. Order by total_amount descending, then user_id ascending.
Only users in banned_users with at least one qualifying transaction are included. Transactions on the ban date itself are excluded.
Input Table(s)
transactions has no primary key. A user can have many rows, including identical ones.
| Column | Type | Meaning |
|---|
| user_id | INT | The user who made the purchase |
| purchase_date | DATE | The day of the purchase |
| amount | DECIMAL(10,2) | The purchase amount, non-negative |
banned_users has primary key user_id, so each user has at most one ban date.
| Column | Type | Meaning |
|---|
| user_id | INT | Primary key. The banned user |
| ban_date | DATE | The day the ban took effect |
Some users in transactions are not banned, and some banned users have no transactions.
Output Contract
-
Columns, in this order:
user_id
,
total_amount
.
-
One row per banned user who has at least one transaction dated strictly before their
ban_date
.
-
total_amount
is the exact sum of those transactions'
amount
, with no rounding. It keeps the two decimal places of
DECIMAL(10,2)
.
-
Order by
total_amount
descending, then
user_id
ascending. Each
user_id
appears once, so the order is fully determined.
Example
Input table transactions:
| user_id | purchase_date | amount |
|---|
| 1 | 2024-01-10 | 50.00 |
| 1 | 2024-02-15 | 30.00 |
| 1 | 2024-03-20 | 25.00 |
| 2 | 2024-01-05 | 100.00 |
| 2 | 2024-02-01 | 40.00 |
| 3 | 2024-02-01 | 75.00 |
Input table banned_users:
| user_id | ban_date |
|---|
| 1 | 2024-03-01 |
| 2 | 2024-02-01 |
| 4 | 2024-04-01 |
Expected output:
| user_id | total_amount |
|---|
| 2 | 100.00 |
| 1 | 80.00 |
User 1 has two transactions before 2024-03-01 (50.00 + 30.00 = 80.00); the 2024-03-20 purchase comes after the ban. User 2 has one transaction before 2024-02-01 (100.00), and the 40.00 purchase on the ban date itself does not count. User 4 is banned but has no transactions, so it is left out. User 3 is not banned.
Constraints and Clarifications
-
transactions
has 1 to 100,000 rows and
banned_users
has 1 to 10,000 rows.
-
Every
amount
is non-negative and every date is a valid
DATE
, so there are no NULLs to handle.
-
Only purchases dated strictly earlier than the ban date count. A purchase on the ban date is excluded.
-
A banned user whose qualifying purchases add up to 0.00 (for example, one purchase of 0.00 before the ban) still appears, with
total_amount
0.00, because they have a qualifying transaction.
-
Identical transaction rows (same user, date and amount) are separate purchases, and each one counts.
-
Transactions by users who are not in
banned_users
are ignored.