SQL
Perform Conditional Updates Based on Multiple Criteria
Update multiple rows in a single SQL query using a CASE statement to apply different values based on specific conditions in your database.
UPDATE products SET price = CASE
WHEN category = 'Electronics' THEN price * 1.05
WHEN category = 'Books' THEN price * 1.02
ELSE price
END,
last_updated = CURRENT_TIMESTAMP
WHERE is_active = TRUE;
How it works: This query demonstrates how to perform a conditional update on one or more columns using the `CASE` statement. It allows different update logic to be applied to rows based on specific conditions (e.g., category in this example) within a single `UPDATE` statement, making it efficient for bulk conditional changes.