Skip to main content
SQL • LESSON 36

DATE_TRUNC

How do we group timestamps into standard calendar boundaries like months or weeks?

Advanced3 Minutes560 XP
🤔 THE QUESTION

How do we group timestamps into standard calendar boundaries like months or weeks?

💡 WHAT IS IT?

DATE_TRUNC truncates timestamps to specified boundaries (year, month, week, day) for aggregations.

🎯 WHAT IS IT USED FOR?

Monthly active metrics, weekly revenue summaries, cohort analysis, dashboard rollups.

💻 EXAMPLE
SELECT
DATE_TRUNC('month', order_date)
AS order_month,
COUNT(*) AS orders
FROM orders
GROUP BY
DATE_TRUNC('month', order_date);

🎯 Mission Objectives

Practice typing production-grade SQL code for DATE_TRUNC.

  • DATE_TRUNC
  • Time-series grouping
  • Monthly cohorting
  • GROUP BY with dates