Updating Product Prices
You are tasked with managing an e-commerce platform's product inventory. A common operation is to adjust the prices of products based on various factors, such as sales, promotions, or cost changes. This challenge requires you to implement a solution to efficiently update the prices of specific products in your database.
Problem Description
You need to write a SQL UPDATE statement that modifies the price column for a set of products in a Products table. The update should only apply to products that meet certain criteria.
What needs to be achieved:
- Update the
priceof specific products in theProductstable.
Key requirements:
- The update should be targeted, meaning only certain rows should be modified.
- The new price should be applied based on a given condition.
Expected behavior:
- Rows in the
Productstable that satisfy the specifiedWHEREclause conditions should have theirpriceupdated to the new value. - Rows that do not satisfy the conditions should remain unchanged.
Important edge cases to consider:
- What happens if no products match the update criteria? The statement should execute without error and make no changes.
- What if the
Productstable is empty? Similar to the above, no changes should occur.
Examples
Example 1:
Input:
Products table:
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1200 | Electronics |
| 2 | Keyboard | 75 | Electronics |
| 3 | Mouse | 25 | Electronics |
| 4 | Desk Lamp | 50 | Home |
| 5 | Office Chair | 150 | Home |
Update operation: Increase the price of all products in the 'Electronics' category by 10%.
Output:
Products table after update:
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1320 | Electronics |
| 2 | Keyboard | 82.5 | Electronics |
| 3 | Mouse | 27.5 | Electronics |
| 4 | Desk Lamp | 50 | Home |
| 5 | Office Chair | 150 | Home |
Explanation: The prices for Laptop, Keyboard, and Mouse were updated because they belong to the 'Electronics' category. The Desk Lamp and Office Chair prices remained the same.
Example 2:
Input:
Products table (same as Example 1 before update):
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1200 | Electronics |
| 2 | Keyboard | 75 | Electronics |
| 3 | Mouse | 25 | Electronics |
| 4 | Desk Lamp | 50 | Home |
| 5 | Office Chair | 150 | Home |
Update operation: Set the price of the product with product_id 4 to 45.
Output:
Products table after update:
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1200 | Electronics |
| 2 | Keyboard | 75 | Electronics |
| 3 | Mouse | 25 | Electronics |
| 4 | Desk Lamp | 45 | Home |
| 5 | Office Chair | 150 | Home |
Explanation: Only the price for the Desk Lamp (product_id 4) was updated to 45.
Example 3: No matching records
Input:
Products table (same as Example 1 before update):
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1200 | Electronics |
| 2 | Keyboard | 75 | Electronics |
| 3 | Mouse | 25 | Electronics |
| 4 | Desk Lamp | 50 | Home |
| 5 | Office Chair | 150 | Home |
Update operation: Decrease the price of all products in the 'Books' category by 5%.
Output:
Products table after update (no changes):
| product_id | name | price | category |
|---|---|---|---|
| 1 | Laptop | 1200 | Electronics |
| 2 | Keyboard | 75 | Electronics |
| 3 | Mouse | 25 | Electronics |
| 4 | Desk Lamp | 50 | Home |
| 5 | Office Chair | 150 | Home |
Explanation: Since no products have the category 'Books', the UPDATE statement had no effect.
Constraints
- The
Productstable will have at least the columnsproduct_id(integer),name(string),price(numeric/decimal), andcategory(string). - The
pricecolumn will always contain a non-negative value. - The
product_idcolumn is the primary key and is unique. - The
UPDATEstatement should be efficient and complete its execution within a reasonable time for tables with up to 100,000 rows.
Notes
- Focus on constructing the correct
UPDATEstatement. - Consider how to express the update logic using the
SETandWHEREclauses of an SQLUPDATEstatement. - For calculations like percentage increases, remember to perform the arithmetic correctly (e.g.,
price * 1.10for a 10% increase).