Join Orders to Discounts on a Non-Unique Product Key and Return Distinct Rows
Company: Snapchat
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Two tables share a `product_id` column, and the value is not unique in either of them. Join `orders` with `discounts` on `product_id` and return the distinct combinations of `order_id`, `product_id`, `quantity` and `discount_pct`. Order the result by `order_id` ascending, then by `discount_pct` ascending.
```hint Count the joined rows
Work out how many joined rows one order produces when its product has two discount rows with the same percentage, and which of those rows the output should keep.
```
### Input Table(s)
Table `orders`:
| Column | Type | Meaning |
|---|---|---|
| `order_id` | INT | Order identifier |
| `product_id` | INT | Product on the order; many orders can share a product |
| `quantity` | INT | Units ordered, a positive integer |
Key: `order_id` is unique in `orders`.
Table `discounts`:
| Column | Type | Meaning |
|---|---|---|
| `discount_id` | INT | Discount identifier |
| `product_id` | INT | Product the discount applies to; a product can have several discount rows |
| `discount_pct` | DECIMAL(5,2) | Discount percentage, from 0.00 to 100.00 |
Key: none declared. `product_id` repeats, and one product can carry the same `discount_pct` on more than one row.
### Output Contract
- Columns, in this order: `order_id`, `product_id`, `quantity`, `discount_pct`.
- Exactly one row for each distinct combination of those four values produced by the join.
- Sorted by `order_id` ascending, then `discount_pct` ascending. Since `order_id` is unique in `orders`, no two output rows share both values, so the order is fully determined.
- `discount_pct` is returned as stored, with two decimal places and no rounding.
### Example
`orders`:
| order_id | product_id | quantity |
|---|---|---|
| 1 | 10 | 2 |
| 2 | 10 | 1 |
| 3 | 20 | 5 |
| 4 | 30 | 1 |
`discounts`:
| discount_id | product_id | discount_pct |
|---|---|---|
| 100 | 10 | 5.00 |
| 101 | 10 | 15.00 |
| 102 | 10 | 5.00 |
| 103 | 20 | 10.00 |
| 104 | 40 | 20.00 |
Expected result:
| order_id | product_id | quantity | discount_pct |
|---|---|---|---|
| 1 | 10 | 2 | 5.00 |
| 1 | 10 | 2 | 15.00 |
| 2 | 10 | 1 | 5.00 |
| 2 | 10 | 1 | 15.00 |
| 3 | 20 | 5 | 10.00 |
Product 10 has three discount rows, two of them at 5.00, so orders 1 and 2 each join to three rows and keep two once duplicates are removed. Order 4 has no discount for product 30 and does not appear, and discount 104 has no order for product 40.
### Constraints and Clarifications
- Each table has between 1 and 10,000 rows.
- `order_id` is unique in `orders`. `product_id` is not unique in either table.
- `quantity` is a positive integer and `discount_pct` lies between 0.00 and 100.00, so neither is NULL.
- The join is an inner join: an order whose product has no discount row is left out, and so is a discount whose product has no order. A NULL `product_id`, if one occurs, matches nothing.
- Repeated rows in the joined result come from `discounts` rows that give one product the same `discount_pct` under different `discount_id` values. Each such combination appears once in the output.
Overview: Join an orders table to a discounts table on a product key that repeats in both, then return each distinct order and discount combination in a fixed order. It tests many-to-many join fan-out, what SELECT DISTINCT removes, and deterministic sorting.