Skip to main content
SQL โ€ข LESSON 36

DATE_TRUNC

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

Advancedโฑ 3 Minutesโญ 560 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