Quick 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.

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.

The `contractors` table has one row per contractor: `contractor_id` (INTEGER, primary key) and `country` (VARCHAR(64), not null). The `payments` table has one row per payment: `payment_id` (INTEGER, primary key), `contractor_id` (INTEGER, not null, always matching a row in `contractors`), `amount` (DECIMAL(12,2), not null and non-negative) and `payment_date` (DATE, not null, with no time of day). Report the total amount paid per country for payments with `payment_date` from 2025-01-01 through 2025-01-31, with both ends of the window included. Return the columns `country` and `total_amount`, in that order, with one row per country that has at least one payment in that window. `total_amount` is the sum of `amount` over those payments, returned with two decimal places and no further rounding. Payments to several contractors in the same country add into one row for that country, and a payment of 0.00 inside the window still counts. A country with no payment in the window does not appear. Sort by `total_amount` descending, then `country` ascending.

Tables

contractors(contractor_id INTEGER, country VARCHAR(64))

payments(payment_id INTEGER, contractor_id INTEGER, amount DECIMAL(12,2), payment_date DATE)

Hints

  1. 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.
  2. The start of the window works the same way: 2025-01-01 counts and 2024-12-31 does not. January payments from other years stay out of the report.
DeelData ScientisteasyData Manipulation (SQL/Python)

Total Contractor Payments per Country for January 2025

The contractors table has one row per contractor: contractor_id (INTEGER, primary key) and country (VARCHAR(64), not null). The payments table has one row per payment: payment_id (INTEGER, primary key), contractor_id (INTEGER, not null, always matching a row in contractors), amount (DECIMAL(12,2), not null and non-negative) and payment_date (DATE, not null, with no time of day).

Report the total amount paid per country for payments with payment_date from 2025-01-01 through 2025-01-31, with both ends of the window included. Return the columns country and total_amount, in that order, with one row per country that has at least one payment in that window. total_amount is the sum of amount over those payments, returned with two decimal places and no further rounding. Payments to several contractors in the same country add into one row for that country, and a payment of 0.00 inside the window still counts. A country with no payment in the window does not appear. Sort by total_amount descending, then country ascending.

Expected Output

Tables

contractorspayments
contractors
ColumnType
contractor_idINTEGER
countryVARCHAR(64)
payments
ColumnType
payment_idINTEGER
contractor_idINTEGER
amountDECIMAL(12,2)
payment_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions