Total Monthly Data Usage per Customer in 2024

Quick Overview

Write a SQL query that joins customers to their traffic usage records and reports each customer's total data usage for every month of 2024. It tests date filtering across year boundaries, grouping by customer and month, join behavior for customers without usage, and rounding of decimal totals.

Total Monthly Data Usage per Customer in 2024

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Write a SQL query that returns each customer's total `data_usage_mb` for each month of 2024. Only usage dated in 2024 counts. Each output row is one customer in one month, with the customer's name and the total usage rounded to 2 decimals. ### Input Table(s) `customers` | Column | Type | Meaning | | --- | --- | --- | | `customer_id` | INT | Primary key | | `customer_name` | VARCHAR(100) | Customer name, never NULL | `traffic_usage` | Column | Type | Meaning | | --- | --- | --- | | `usage_id` | INT | Primary key, one row per usage record | | `customer_id` | INT | Foreign key to `customers.customer_id` | | `usage_date` | DATE | Date of the usage, never NULL | | `data_usage_mb` | DECIMAL(10,2) | Data used in megabytes, never NULL and never negative | A customer can have zero, one or many usage records in a month. ### Output Contract | Column | Type | Meaning | | --- | --- | --- | | `customer_id` | INTEGER | The customer | | `customer_name` | TEXT | Name from `customers` | | `Year` | INTEGER | Calendar year of the usage, always 2024 | | `Month` | INTEGER | Calendar month of the usage, 1 to 12 | | `TotalUsageMB` | DECIMAL | Sum of `data_usage_mb` for that customer and month, rounded to 2 decimals | Column names are case-sensitive and must match exactly. Order by `customer_id` ascending, then `Month` ascending. Each (`customer_id`, `Month`) pair appears once, so this order is fully determined. ### Example Input `customers`: | customer_id | customer_name | | --- | --- | | 1 | Acme Corp | | 2 | Blue Fin Labs | | 3 | Cedar Health | Input `traffic_usage`: | usage_id | customer_id | usage_date | data_usage_mb | | --- | --- | --- | --- | | 1 | 1 | 2024-01-05 | 120.50 | | 2 | 1 | 2024-01-20 | 79.50 | | 3 | 1 | 2024-03-02 | 300.00 | | 4 | 2 | 2023-12-31 | 500.00 | | 5 | 2 | 2024-01-01 | 10.25 | | 6 | 2 | 2025-01-01 | 42.00 | | 7 | 3 | 2023-06-15 | 88.00 | Expected output: | customer_id | customer_name | Year | Month | TotalUsageMB | | --- | --- | --- | --- | --- | | 1 | Acme Corp | 2024 | 1 | 200.00 | | 1 | Acme Corp | 2024 | 3 | 300.00 | | 2 | Blue Fin Labs | 2024 | 1 | 10.25 | Acme's two January records add to 120.50 + 79.50 = 200.00, and Acme has no February row because it had no February usage. Blue Fin Labs' records on 2023-12-31 and 2025-01-01 fall outside 2024. Cedar Health has no 2024 usage, so it does not appear. ### Constraints and Clarifications - `customers` has at most 10,000 rows and `traffic_usage` at most 100,000 rows. - `usage_date` holds valid dates spanning several years; only 2024 dates count. - `data_usage_mb` is non-negative. - Report only (customer, month) pairs with at least one 2024 usage record. Do not output zero rows for months without usage, and do not list customers with no 2024 usage. - Usage rows whose `customer_id` does not match a customer are not reported. ```hint Filter first Restrict the usage rows to 2024 before aggregating, then decide which columns together identify one output row. ```

Overview: Write a SQL query that joins customers to their traffic usage records and reports each customer's total data usage for every month of 2024. It tests date filtering across year boundaries, grouping by customer and month, join behavior for customers without usage, and rounding of decimal totals.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Write a SQL query that returns each customer's total data_usage_mb for each month of 2024. Only usage dated in 2024 counts. Each output row is one customer in one month, with the customer's name and the total usage rounded to 2 decimals.

Input Table(s)

customers

ColumnTypeMeaning
customer_idINTPrimary key
customer_nameVARCHAR(100)Customer name, never NULL

traffic_usage

ColumnTypeMeaning
usage_idINTPrimary key, one row per usage record
customer_idINTForeign key to customers.customer_id
usage_dateDATEDate of the usage, never NULL
data_usage_mbDECIMAL(10,2)Data used in megabytes, never NULL and never negative

A customer can have zero, one or many usage records in a month.

Output Contract

ColumnTypeMeaning
customer_idINTEGERThe customer
customer_nameTEXTName from customers
YearINTEGERCalendar year of the usage, always 2024
MonthINTEGERCalendar month of the usage, 1 to 12
TotalUsageMBDECIMALSum of data_usage_mb for that customer and month, rounded to 2 decimals

Column names are case-sensitive and must match exactly. Order by customer_id ascending, then Month ascending. Each (customer_id, Month) pair appears once, so this order is fully determined.

Example

Input customers:

customer_idcustomer_name
1Acme Corp
2Blue Fin Labs
3Cedar Health

Input traffic_usage:

usage_idcustomer_idusage_datedata_usage_mb
112024-01-05120.50
212024-01-2079.50
312024-03-02300.00
422023-12-31500.00
522024-01-0110.25
622025-01-0142.00
732023-06-1588.00

Expected output:

customer_idcustomer_nameYearMonthTotalUsageMB
1Acme Corp20241200.00
1Acme Corp20243300.00
2Blue Fin Labs2024110.25

Acme's two January records add to 120.50 + 79.50 = 200.00, and Acme has no February row because it had no February usage. Blue Fin Labs' records on 2023-12-31 and 2025-01-01 fall outside 2024. Cedar Health has no 2024 usage, so it does not appear.

Constraints and Clarifications

  • customers has at most 10,000 rows and traffic_usage at most 100,000 rows.
  • usage_date holds valid dates spanning several years; only 2024 dates count.
  • data_usage_mb is non-negative.
  • Report only (customer, month) pairs with at least one 2024 usage record. Do not output zero rows for months without usage, and do not list customers with no 2024 usage.
  • Usage rows whose customer_id does not match a customer are not reported.