Total Contractor Payments by Country for January 2025
Company: Deel
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Report the total amount paid per country for payments with `payment_date` from `2025-01-01` through `2025-01-31` inclusive. Return `country` and `total_amount`, ordered by `total_amount` descending, then `country` ascending.
```hint Check both ends of the window
A payment dated 2025-01-31 belongs in the report and one dated 2025-02-01 does not. Make sure your date condition treats both edges that way.
```
### Input Table(s)
Table `Contractors`:
| Column | Type | Meaning |
|---|---|---|
| `contractor_id` | INT | Contractor identifier |
| `country` | VARCHAR(64) | Contractor's country, not null |
Key: `contractor_id` is the primary key.
Table `Payments`:
| Column | Type | Meaning |
|---|---|---|
| `payment_id` | INT | Payment identifier |
| `contractor_id` | INT | Contractor who was paid, not null |
| `amount` | DECIMAL(12,2) | Amount paid, not null and non-negative |
| `payment_date` | DATE | Date of the payment, not null |
Keys: `payment_id` is the primary key. Every `contractor_id` matches a row in `Contractors`.
### Output Contract
- Columns, in this order: `country`, `total_amount`.
- One row per country that has at least one payment dated from 2025-01-01 through 2025-01-31.
- `total_amount` is the sum of `amount` over those payments, returned with two decimal places and no further rounding.
- Sorted by `total_amount` descending, then `country` ascending. Each country appears once, so the order is fully determined.
### Example
`Contractors`:
| contractor_id | country |
|---|---|
| 1 | US |
| 2 | DE |
| 3 | US |
| 4 | BR |
| 5 | IN |
`Payments`:
| payment_id | contractor_id | amount | payment_date |
|---|---|---|---|
| 101 | 1 | 1000.00 | 2025-01-05 |
| 102 | 3 | 500.00 | 2025-01-31 |
| 103 | 2 | 1500.00 | 2025-01-15 |
| 104 | 4 | 700.00 | 2025-02-01 |
| 105 | 4 | 800.00 | 2024-12-31 |
| 106 | 5 | 900.00 | 2025-01-01 |
| 107 | 2 | 0.00 | 2025-01-20 |
Expected result:
| country | total_amount |
|---|---|
| DE | 1500.00 |
| US | 1500.00 |
| IN | 900.00 |
US adds two contractors' payments (1000.00 and 500.00), and DE adds 1500.00 and 0.00, so both total 1500.00 and are ordered by country name. Both BR payments fall outside January 2025, so BR has no row.
### Constraints and Clarifications
- `Contractors` has between 1 and 10,000 rows, and `Payments` has between 1 and 100,000 rows.
- `amount` is non-negative, and no column used here is NULL.
- Every `contractor_id` in `Payments` exists in `Contractors`.
- Both ends of the window are inclusive. `payment_date` is a `DATE`, with no time of day.
- A country with no payment in the window does not appear; there are no rows with a zero total for missing countries.
- Payments to several contractors in the same country add into one row for that country. A payment of 0.00 inside the window still counts.
Overview: Write a SQL query that totals contractor payments per country for January 2025 and ranks the countries by that total. It tests joining payments to contractors, filtering an inclusive date window, grouping by country, and ordering with a tiebreaker.