SQL
Deleting Old Records Based on Timestamp
Learn how to remove outdated data from a SQL table by deleting records older than a specified time frame using a timestamp column.
DELETE FROM logs
WHERE created_at < NOW() - INTERVAL '3 months';
-- For MySQL, use DATE_SUB(NOW(), INTERVAL 3 MONTH)
-- For SQL Server, use DATEADD(month, -3, GETDATE())
How it works: This query deletes records from the `logs` table that are older than three months. It compares the `created_at` timestamp of each log entry with the current timestamp (`NOW()`) minus a three-month interval. This is a common database maintenance task to manage data growth and adhere to data retention policies. Note the comments for database-specific date function variations.