Percentage of Orders Shipped to the User's Primary Address vs Other Addresses
Company: Adobe
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each row of `transactions` is one order. Compute the percentage of orders shipped to the user's primary address and the percentage shipped to other addresses. An order counts as primary when `transactions.shipping_address` exactly matches that user's `users.primary_address`. Return a single row with `primary_address_pct` and `other_address_pct`, both with 2 decimals and summing to 100.00.
```hint Watch the division
In PostgreSQL, dividing one integer count by another truncates the result. Check the type of every value in your percentage expression.
```
### Input Table(s)
Table `users`
| Column | Type | Meaning |
|---|---|---|
| user_id | INT | Id of the user. Primary key. |
| name | VARCHAR(100) | The user's name. May be NULL. |
| primary_address | VARCHAR(255) | The user's primary address. NOT NULL. |
Table `transactions`
| Column | Type | Meaning |
|---|---|---|
| transaction_id | INT | Id of the order. Primary key. |
| user_id | INT | User who placed the order. Foreign key to `users.user_id`. |
| shipping_address | VARCHAR(255) | Address the order shipped to. NOT NULL. |
| order_date | DATE | Date of the order. NOT NULL. |
| amount | DECIMAL(10,2) | Order amount. May be NULL. |
### Output Contract
- Exactly one row with two columns, in this order: `primary_address_pct`, `other_address_pct`.
- `primary_address_pct` is 100 times the number of orders whose `shipping_address` equals the user's `primary_address`, divided by the total number of orders, rounded to 2 decimals with PostgreSQL `ROUND` on a numeric value (halves round away from zero).
- `other_address_pct` is 100.00 minus the rounded `primary_address_pct`. Deriving it from the rounded value guarantees the two columns sum to exactly 100.00.
- Both columns are numeric with two decimal places.
### Example
Input `users`:
| user_id | name | primary_address |
|---|---|---|
| 1 | Ana | 12 Oak St |
| 2 | Ben | 9 Elm Ave |
| 3 | Cy | 4 Pine Rd |
Input `transactions`:
| transaction_id | user_id | shipping_address | order_date | amount |
|---|---|---|---|---|
| 101 | 1 | 12 Oak St | 2024-01-05 | 40.00 |
| 102 | 1 | 500 Market St | 2024-01-09 | 25.00 |
| 103 | 2 | 9 Elm Ave | 2024-02-01 | 60.00 |
| 104 | 2 | 9 Elm Ave | 2024-02-14 | NULL |
| 105 | 3 | 4 pine rd | 2024-03-02 | 15.00 |
| 106 | 3 | 4 Pine Rd | 2024-03-10 | 30.00 |
Expected output:
| primary_address_pct | other_address_pct |
|---|---|
| 66.67 | 33.33 |
Orders 101, 103, 104 and 106 match the user's primary address exactly, which is 4 of 6 orders, or 66.666... percent, rounded to 66.67. Order 105 differs only in letter case, so it counts as another address along with order 102. The other share is 100.00 minus 66.67, which is 33.33.
### Constraints and Clarifications
- `users` has at most 10,000 rows and `transactions` at most 100,000 rows.
- Every `user_id` in `transactions` exists in `users`.
- You may assume `transactions` has at least one row.
- Every transaction row is one order, whatever its `amount`. Rows with a NULL `amount` still count.
- Matching is exact string equality. Case, spacing and punctuation must all match, so `'4 pine rd'` is not `'4 Pine Rd'`.
- Rounding each share on its own can break the sum. With 160 orders of which 1 is primary, independent rounding gives 0.63 and 99.38 (total 100.01); the contract above gives 0.63 and 99.37.
Overview: Write a SQL query that reports what share of orders shipped to each buyer's primary address and what share went elsewhere, as two percentages that add up to exactly 100.00. It tests joins, conditional counting, numeric division and consistent rounding.