Records from Last 7 Days in MySQL
In this lesson: Fetch records from the last 7 days, 30 days or this month in MySQL using DATE_SUB and CURDATE — with output. In Hindi & English.
Fetching recent rows
A very common report is "show records from the last 7 days" — recent orders, sign-ups or logs. MySQL makes this easy with CURDATE() and INTERVAL, and the same pattern scales to 30 days, today, or any period you need.
date_column >= CURDATE() - INTERVAL 7 DAY — it stays correct every day.Last 7 days
SELECT * FROM orders WHERE order_date >= CURDATE() - INTERVAL 7 DAY;
This returns every order dated within the last week. Because it uses CURDATE(), it automatically means "the last 7 days" no matter which day you run it.
How INTERVAL works
SELECT CURDATE(), CURDATE() - INTERVAL 7 DAY;
INTERVAL 7 DAY shifts a date by seven days. Subtracting it from today gives the date a week ago, which becomes the lower bound of the range.
Just today
SELECT * FROM orders WHERE DATE(created_at) = CURDATE();
To get only today's rows, compare the date part to CURDATE(). Wrapping the column in DATE() ignores the time, so every row created today matches, whatever the clock time.
Last 30 days
SELECT * FROM orders WHERE order_date >= CURDATE() - INTERVAL 30 DAY;
Just change the number and unit. The same pattern gives the last 30 days, last 3 months (INTERVAL 3 MONTH), or last year (INTERVAL 1 YEAR).
With date-time columns
SELECT * FROM logs WHERE created_at >= NOW() - INTERVAL 7 DAY;
If the column stores a full date and time, use NOW() (which includes the time) instead of CURDATE(). This gives records from exactly 7 days ago to this moment.
Common mistakes
- Using a fixed date. It goes stale; CURDATE()/NOW() stay current.
- Ignoring the time part. Use DATE() or NOW() to match how the column stores time.
- Wrong interval unit. DAY, MONTH, YEAR must match your intent.
- Comparing a date to a string. Store dates in DATE/DATETIME columns.
Summary
- Get the last 7 days with
>= CURDATE() - INTERVAL 7 DAY. - INTERVAL shifts a date by a number of days, months or years.
- Use
DATE(col) = CURDATE()for just today. - Change the number/unit for 30 days, a month, a year, etc.
- Use NOW() instead of CURDATE() for date-time columns.
Next, get month boundaries in first and last day of a month, or compute ages in age from date of birth.
Frequently Asked Questions
How do I get records from the last 7 days in MySQL?
WHERE date_column >= CURDATE() - INTERVAL 7 DAY. This returns all rows dated within the past week, up to today. It updates automatically each day because it is based on CURDATE().What does INTERVAL do in MySQL?
CURDATE() - INTERVAL 7 DAY gives the date 7 days ago. You can use DAY, MONTH, YEAR and other units to shift dates for range queries.How do I get only today's records?
WHERE DATE(created_at) = CURDATE(). Using DATE() strips the time, so all of today's rows match regardless of the time they were created.How do I get records from the last 30 days?
WHERE date_column >= CURDATE() - INTERVAL 30 DAY. The same pattern works for any number of days, months or years by adjusting the INTERVAL value and unit.