SQL
Archive or Delete Old Records Based on Date Range
Efficiently manage database size by archiving or deleting records older than a specified date range using SQL, crucial for data retention policies.
INSERT INTO archived_orders
SELECT * FROM orders
WHERE order_date < NOW() - INTERVAL '5 years';
DELETE FROM orders
WHERE order_date < NOW() - INTERVAL '5 years';
How it works: This two-part snippet demonstrates a common data retention strategy. First, it `INSERT`s records from the `orders` table that are older than 5 years into an `archived_orders` table. Subsequently, it `DELETE`s those same old records from the main `orders` table, helping to maintain database performance and manage storage space according to data retention policies. The exact date/interval functions may vary slightly by SQL dialect.