You are given two tables: Products and Discounts.
The Products table contains information about products including their ID, category, and price. The Discounts table contains the discount percentage for each product category.
Write a SQL query to calculate the final price of each product after applying the category discount. If a product's category has no associated discount, its price remains unchanged.
Return the result table ordered by product_id in ascending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Unique identifier for each product |
category
|
varchar | Product category |
price
|
decimal | Original product price |
| Column Name | Type | Description |
|---|---|---|
category
PK
|
varchar | Product category |
discount
|
int | Discount percentage (0-100) |
Input & Output
| product_id | category | price |
|---|---|---|
| 1 | Electronics | 1000 |
| 2 | Clothing | 50 |
| 3 | Electronics | 1200 |
| 4 | Home | 500 |
| category | discount |
|---|---|
| Electronics | 10 |
| Clothing | 20 |
| product_id | final_price | category |
|---|---|---|
| 1 | 900 | Electronics |
| 2 | 40 | Clothing |
| 3 | 1080 | Electronics |
| 4 | 500 | Home |
Electronics products get 10% discount (1000 → 900, 1200 → 1080), Clothing gets 20% discount (50 → 40), and Home category has no discount so price remains 500.
| product_id | category | price |
|---|---|---|
| 1 | Books | 25 |
| 2 | Sports | 100 |
| category | discount |
|---|---|
| Books | 15 |
| Sports | 25 |
| product_id | final_price | category |
|---|---|---|
| 1 | 21.25 | Books |
| 2 | 75 | Sports |
Books get 15% discount (25 → 21.25) and Sports get 25% discount (100 → 75). All products have matching discount categories.
| product_id | category | price |
|---|---|---|
| 1 | Jewelry | 200 |
| 2 | Art | 500 |
| category | discount |
|---|
| product_id | final_price | category |
|---|---|---|
| 1 | 200 | Jewelry |
| 2 | 500 | Art |
No discounts are available for any category, so all products retain their original prices.
Constraints
-
1 ≤ product_id ≤ 1000 -
categoryis a non-empty string -
price > 0 -
0 ≤ discount ≤ 100