⏱️ DATEDIFF() – Calculate Days Between Two Dates in SQL

🔧 Quick Syntax

SELECT DATEDIFF(date1, date2);

This tells SQL:

“Tell me how many days are between date1 and date2.”

Note: SQL does date1 - date2 → so:

  • If date1 is newer, the result is positive
  • If date1 is older, the result is negative

🧾 Here’s the Table You’re Working With

Your table is called orders:

id customer order_date
1Fenn2025-04-10
2Milo2025-04-14
3Sora2025-04-01
4Lexa2025-03-25
5Tovi2025-04-12

✅ Suppose You Want to Know How Many Days Ago Each Order Was Placed

SELECT customer, DATEDIFF(CURDATE(), order_date) AS days_ago
FROM orders;

💡 Output (if today is 2025-04-14):

Fenn    → 4
Milo    → 0
Sora    → 13
Lexa    → 20
Tovi    → 2

This shows how many days have passed since each order was placed — great for time-based reporting.


✅ Suppose You Want to See Orders Older Than 7 Days

SELECT *
FROM orders
WHERE DATEDIFF(CURDATE(), order_date) > 7;

💡 Output:

Sora
Lexa

Only those two orders are more than 7 days old — perfect for follow-up reminders, alerts, or overdue filters.


🧃 Recap – What You Learned

  • DATEDIFF() returns the number of days between two dates
  • Positive if the first date is newer
  • Use CURDATE() to compare with today
  • Great for tracking age, delays, reminders, or due dates
  • Works in SELECT, WHERE, or inside calculations

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top