Total Each Banned User's Purchases Made Before Their Ban Date

Quick Overview

Write a SQL query that totals each banned user's purchases made strictly before their ban date and leaves out banned users with no earlier purchases. It tests date conditions inside a join, how an inner join filters rows, aggregation, and ordering the result so it is fully determined.

Total Each Banned User's Purchases Made Before Their Ban Date

Company: Ozon

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Trace the edge rows Before you trust your query, run user 4 from the example (banned, never bought anything) and user 2's purchase dated exactly on the ban date through it by hand. ``` ### 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.

Overview: Write a SQL query that totals each banned user's purchases made strictly before their ban date and leaves out banned users with no earlier purchases. It tests date conditions inside a join, how an inner join filters rows, aggregation, and ordering the result so it is fully determined.

Ozon logo
Ozon
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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.

ColumnTypeMeaning
user_idINTThe user who made the purchase
purchase_dateDATEThe day of the purchase
amountDECIMAL(10,2)The purchase amount, non-negative

banned_users has primary key user_id, so each user has at most one ban date.

ColumnTypeMeaning
user_idINTPrimary key. The banned user
ban_dateDATEThe 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_idpurchase_dateamount
12024-01-1050.00
12024-02-1530.00
12024-03-2025.00
22024-01-05100.00
22024-02-0140.00
32024-02-0175.00

Input table banned_users:

user_idban_date
12024-03-01
22024-02-01
42024-04-01

Expected output:

user_idtotal_amount
2100.00
180.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.