Total sales per region for the Electronics category
Company: Qualcomm
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
A retailer records each sale in `sales`, the buyer's region in `customers`, and each product's category in `products`. Find the total sales amount per `region` for the category `'Electronics'`, where each sale's amount is `quantity * unit_price`. Return columns `region`, `category`, `total_sales`, ordered by `total_sales` descending, then `region` ascending.
```hint Where the multiplication goes
Compute the amount on each sale row before you add anything up. `SUM(quantity) * SUM(unit_price)` is a different number from the total you want.
```
### Input Table(s)
`sales` (one row per sale)
| Column | Type | Meaning |
|---|---|---|
| sale_id | INTEGER | Primary key |
| customer_id | INTEGER | Buyer, references `customers.customer_id` |
| product_id | INTEGER | Product sold, references `products.product_id` |
| quantity | INTEGER | Units sold, positive |
| unit_price | NUMERIC(10,2) | Price per unit on this sale, positive |
`customers`
| Column | Type | Meaning |
|---|---|---|
| customer_id | INTEGER | Primary key |
| region | VARCHAR | Sales region of the customer |
`products`
| Column | Type | Meaning |
|---|---|---|
| product_id | INTEGER | Primary key |
| category | VARCHAR | Product category, such as `'Electronics'`, `'Clothing'`, `'Food'`, `'Furniture'` |
### Output Contract
- Columns, in this order: `region`, `category`, `total_sales`.
- One row per region that has at least one Electronics sale. Regions with no Electronics sales do not appear.
- `category` is `'Electronics'` on every row.
- `total_sales` is the sum of `quantity * unit_price` over that region's Electronics sales, as an exact decimal with 2 decimal places. No further rounding is needed.
- Order by `total_sales` descending, then `region` ascending. Each region appears once, so the order is fully determined.
### Example
`customers`
| customer_id | region |
|---|---|
| 1 | East |
| 2 | West |
| 3 | East |
| 4 | North |
| 5 | South |
`products`
| product_id | category |
|---|---|
| 10 | Electronics |
| 11 | Electronics |
| 12 | Clothing |
| 13 | Food |
`sales`
| sale_id | customer_id | product_id | quantity | unit_price |
|---|---|---|---|---|
| 100 | 1 | 10 | 2 | 150.00 |
| 101 | 3 | 11 | 1 | 200.00 |
| 102 | 2 | 10 | 4 | 125.00 |
| 103 | 2 | 12 | 10 | 30.00 |
| 104 | 4 | 11 | 3 | 49.99 |
| 105 | 5 | 13 | 5 | 12.00 |
| 106 | 4 | 12 | 1 | 80.00 |
Expected result:
| region | category | total_sales |
|---|---|---|
| East | Electronics | 500.00 |
| West | Electronics | 500.00 |
| North | Electronics | 149.97 |
East has two Electronics sales from two different customers, `2 * 150.00` and `1 * 200.00`, which sum to 500.00. West's only Electronics sale is `4 * 125.00 = 500.00`; its Clothing sale (103) is excluded. East and West tie, so `region` ascending puts East first. North's Electronics sale is `3 * 49.99 = 149.97`, and South bought only Food, so South has no row.
### Constraints and Clarifications
- `sales` has at most 10,000 rows; `customers` and `products` have at most 1,000 rows each.
- `quantity` is a positive integer and `unit_price` is a positive decimal with 2 decimal places.
- Every `sales` row has a `customer_id` and a `product_id` that exist in `customers` and `products`. `region` and `category` are never NULL.
- The category match is exact and case-sensitive: only `'Electronics'` counts.
- Each `sales` row is a separate sale. Repeat purchases of the same product by the same customer all count toward the total.
Overview: Write a SQL query that joins sales, customers and products to total Electronics revenue by region, where each sale's amount is quantity times unit price. It tests multi-table joins, filtering before aggregation, GROUP BY, and a deterministic sort with a tiebreaker.