MySQL + SQL · Lesson 1
Records Last 7 Days
Recent Records
To get rows created in the last 7 days, compare the date column with the date 7 days ago using DATE_SUB.
The Query
SELECT * FROM orders
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);
How it Works
DATE_SUB(CURDATE(), INTERVAL 7 DAY) gives the date 7 days ago. Any row with a newer date is from the last week.Summary
- Use
created_at >= DATE_SUB(CURDATE(), INTERVAL 7 DAY). - Change 7 to any number of days you need.