Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 149

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.

💡 Use date_column >= CURDATE() - INTERVAL 7 DAY — it stays correct every day.

Last 7 days

SELECT * FROM orders
WHERE order_date >= CURDATE() - INTERVAL 7 DAY;
-- returns all orders from the past 7 days up to today

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;
+------------+------------------------------+ | CURDATE() | CURDATE() - INTERVAL 7 DAY | +------------+------------------------------+ | 2026-09-21 | 2026-09-14 | +------------+------------------------------+

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?
Use 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?
INTERVAL lets you add or subtract time from a date. 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?
Compare the date part to CURDATE(): 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?
Change the interval: 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.
Why should I use CURDATE() instead of a fixed date?
CURDATE() always returns today, so the query stays correct every day without editing. A fixed date would need updating constantly. Using CURDATE() with INTERVAL makes "recent" ranges self-maintaining.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicInsert from Another Table Your progress: 149 / 151 (99%) · Lesson 150 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free