Table: Products
+------------+---------+ | Column Name| Type | +------------+---------+ | product_id | int | | category | varchar | | price | decimal | +------------+---------+ product_id is the unique key for this table. Each row includes the product's ID, its category, and its price.
Table: Discounts
+------------+---------+ | Column Name| Type | +------------+---------+ | category | varchar | | discount | int | +------------+---------+ category is the primary key for this table. Each row contains a product category and the percentage discount applied to that category (values range from 0 to 100).
Write a solution to find 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.
The result format is in the following example.
Example:
Input:
Products table:
+------------+-------------+-------+ | product_id | category | price | +------------+-------------+-------+ | 1 | Electronics | 1000 | | 2 | Clothing | 50 | | 3 | Electronics | 1200 | | 4 | Home | 500 | +------------+-------------+-------+
Discounts table:
+------------+----------+ | category | discount | +------------+----------+ | Electronics| 10 | | Clothing | 20 | +------------+----------+
Output:
+------------+------------+-------------+ | product_id | final_price| category | +------------+------------+-------------+ | 1 | 900 | Electronics | | 2 | 40 | Clothing | | 3 | 1080 | Electronics | | 4 | 500 | Home | +------------+------------+-------------+
Explanation:
Loading editor...
{"headers":{"Products":["product_id","category","price"],"Discounts":["category","discount"]},"rows":{"Products":[[1,"Electronics",1000],[2,"Clothing",50],[3,"Electronics",1200],[4,"Home",500]],"Discounts":[["Electronics",10],["Clothing",20]]}}