Learn SQL Lesson
Extracting Parts of a Date
You can pull specific parts out of a date with `EXTRACT`.
For example, to get the month number from `order_date`:
SELECT EXTRACT(MONTH FROM order_date) AS order_month
FROM orders
This is useful when you want to group by year, month, quarter, weekday, and more. Be careful when grouping by month number alone: January 2024 and January 2025 would both become month `1`.
For real month-based reports, `DATE_TRUNC` is often safer because it keeps the year and month together by rounding the date down to the start of the month.
SELECT DATE_TRUNC('month', order_date) AS month_start
FROM orders
Practice challenge
Count how many orders were placed in each month. Return order_month and order_count, ordered by order_month.