SQL • LESSON 36
DATE_TRUNC
Learn how to group timestamps into meaningful time periods.
🔴 Advanced⏱ 3 Minutes⭐ 560 XP
🤔 THE QUESTION
How can we group timestamp data by month, day, or another time period?
💡 WHAT IS IT?
DATE_TRUNC truncates a date or timestamp to a specified time boundary such as a day, week, month, or year.
🎯 WHAT IS IT USED FOR?
It is useful for time-based analytics, monthly reports, weekly metrics, daily aggregations, dashboards, and grouping events into consistent time periods.
💻 EXAMPLE
SELECT
DATE_TRUNC('month', order_date)
AS order_month,
COUNT(*) AS orders
FROM orders
GROUP BY
DATE_TRUNC('month', order_date);Mission Brief
Practice truncating order timestamps to monthly boundaries and grouping orders by month.
Objectives
- ✓ DATE_TRUNC
- ✓ Timestamps
- ✓ MONTH
- ✓ GROUP BY
- ✓ COUNT