Find the customer address active on each invoice date
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A billing database keeps the full history of each customer's mailing address. When a customer moves, the old address row gets an end date and a new row starts.
For each invoice, return the customer's address that was active on the `invoice_date`: the address with `effective_from <= invoice_date` and `effective_to` either `NULL` (the current address) or strictly after `invoice_date`. Return `invoice_id`, `customer_name`, `invoice_date`, `amount`, `street`, `city`, `state`, `zip_code`, ordered by `invoice_id` ascending.
```hint Treat each row as an interval
Each address row covers a half-open range of dates. Decide how the open end of the current address should compare with an invoice date before you write the join.
```
### Input Table(s)
`customers` (primary key `customer_id`)
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer identifier |
| customer_name | VARCHAR | Customer's name |
`customer_addresses` (primary key `(customer_id, effective_from)`; `customer_id` references `customers`)
| Column | Type | Meaning |
|---|---|---|
| customer_id | INT | Customer the address belongs to |
| street | VARCHAR | Street line |
| city | VARCHAR | City |
| state | VARCHAR | State code |
| zip_code | VARCHAR | ZIP code, stored as text so leading zeros are kept |
| effective_from | DATE | First day the address is in effect (inclusive) |
| effective_to | DATE | Day the address stops being in effect (exclusive); `NULL` for the current address |
`invoices` (primary key `invoice_id`; `customer_id` references `customers`)
| Column | Type | Meaning |
|---|---|---|
| invoice_id | INT | Invoice identifier |
| customer_id | INT | Billed customer |
| invoice_date | DATE | Date the invoice was issued |
| amount | DECIMAL(10,2) | Invoice amount |
### Output Contract
- Columns, in this order: `invoice_id`, `customer_name`, `invoice_date`, `amount`, `street`, `city`, `state`, `zip_code`.
- Exactly one row per invoice.
- Sort by `invoice_id` ascending. `invoice_id` is unique, so the order is fully determined.
- Return values as stored, with no rounding or reformatting of `amount` or the dates.
### Example
`customers`
| customer_id | customer_name |
|---|---|
| 1 | Alice Chen |
| 2 | Bob Martinez |
`customer_addresses`
| customer_id | street | city | state | zip_code | effective_from | effective_to |
|---|---|---|---|---|---|---|
| 1 | 12 Oak St | Austin | TX | 78701 | 2023-01-01 | 2023-06-15 |
| 1 | 450 Pine Ave | Denver | CO | 80202 | 2023-06-15 | NULL |
| 2 | 9 Elm Rd | Boston | MA | 02108 | 2022-03-01 | NULL |
`invoices`
| invoice_id | customer_id | invoice_date | amount |
|---|---|---|---|
| 101 | 1 | 2023-03-10 | 250.00 |
| 102 | 2 | 2023-04-02 | 80.50 |
| 103 | 1 | 2023-06-15 | 120.00 |
| 104 | 1 | 2023-09-01 | 75.25 |
Expected result:
| invoice_id | customer_name | invoice_date | amount | street | city | state | zip_code |
|---|---|---|---|---|---|---|---|
| 101 | Alice Chen | 2023-03-10 | 250.00 | 12 Oak St | Austin | TX | 78701 |
| 102 | Bob Martinez | 2023-04-02 | 80.50 | 9 Elm Rd | Boston | MA | 02108 |
| 103 | Alice Chen | 2023-06-15 | 120.00 | 450 Pine Ave | Denver | CO | 80202 |
| 104 | Alice Chen | 2023-09-01 | 75.25 | 450 Pine Ave | Denver | CO | 80202 |
Invoice 103 is dated on the day Alice moved. Her Austin row ends on 2023-06-15, and that end date is not strictly after the invoice date, so the invoice belongs to the Denver address that starts that day.
### Constraints and Clarifications
- Address date ranges for a customer do not overlap.
- Every invoice date falls within exactly one address range for that customer.
- `effective_to` is `NULL` for the customer's current (most recent) address.
- When an old row's `effective_to` equals the next row's `effective_from`, an invoice dated that day belongs to the newer address.
- Every `customer_id` in `invoices` exists in `customers`.
- Each table has at most 10,000 rows.
- `amount` is a positive decimal.
Overview: Write a SQL query that pairs each invoice with the customer address in effect on the invoice date, using an address history table with start and end dates. It tests range joins on slowly changing dimension tables, half-open date intervals, and handling a NULL end date for the current address.