Tier 4 · Advanced

Grouping by Date Periods in SQL

By Owen Middleton · Updated September 2026 · Examples run on PostgreSQL 17

How do you group by date periods in SQL?

Grouping by date period means truncating timestamps to a calendar unit — month, week, day — and using the truncated value as the GROUP BY key. The result is one row per period with aggregated measures across all rows that fall within it.

Raw timestamps are almost never the right grouping key. An orders table with a ordered_at timestamp down to the millisecond has a unique timestamp for nearly every row. Grouping by the raw value produces one group per row — not aggregation at all. The useful question is "what happened in March?" not "what happened at 14:32:07.443 on March 3rd?"

How do you group by month in a SQL query?

date_trunc() reduces a timestamp to the start of the specified period. All timestamps in the same period truncate to the same value and group correctly.

SELECT
    date_trunc('month', ordered_at)::date AS month,
    COUNT(*)                              AS order_count,
    SUM(total_amount)                     AS total_revenue
FROM orders
GROUP BY date_trunc('month', ordered_at)
ORDER BY month
SELECT
  date_trunc('month', ordered_at)::date AS month,
  COUNT(*) AS order_count,
  SUM(total_amount) AS revenue
FROM orders
GROUP BY date_trunc('month', ordered_at)
ORDER BY month

This produces one row per month with a count and revenue total. Every order in March 2024 truncates to 2024-03-01 00:00:00 and ends up in the same group.

What can you put in a GROUP BY clause?

The one thing that trips people up

PostgreSQL lets you group by the full expression, by the column's position, or by the output alias. All three of these do the same thing:

GROUP BY date_trunc('month', ordered_at)
GROUP BY 1
GROUP BY month

The trap is what happens when an alias collides with a real column. An input column name wins over an output alias, so SELECT country AS city ... GROUP BY city groups by the table's own city column rather than by the alias you just defined, and the query fails because country is then neither grouped nor aggregated. Give an alias a name that no column in the query already carries and the ambiguity never arises.

WHERE and HAVING are the clauses that genuinely reject an alias, which is probably where the idea comes from. Both are resolved before the SELECT list, so at that point the name does not exist yet.

The output of date_trunc() is a timestamp, even when the input is a date. For display or downstream joins that expect a date type, cast after truncating:

date_trunc('month', ordered_at)::date AS month

Which day does date_trunc treat as the start of a week?

Week truncation starts on Monday

date_trunc('week', ...) follows ISO week convention: weeks begin on Monday. If your reporting context expects Sunday-starting weeks, the truncation will group differently than expected. For most analytical work, aligning to ISO weeks is the practical choice. If you need Sunday weeks, that requires a workaround with date arithmetic.

Grouping by date period is the foundation for almost all time-series work. Period aggregates feed into running totals, period-over-period comparisons, and trend analysis — all of which need clean one-row-per-period output to start from.

Which precision values does date_trunc accept?

Available precision values

date_trunc() supports: 'microseconds', 'milliseconds', 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year', 'decade', 'century'. You'll use 'day', 'week', 'month', 'quarter', and 'year' most often. 'quarter' truncates to the first day of the quarter (January 1, April 1, July 1, October 1), which is useful for quarterly reporting without manual CASE WHEN logic.

Multiple grouping levels in one query

You can compute period aggregates at different levels in the same query using CTEs. Compute daily totals in one CTE, then aggregate those to monthly in the next. This is cleaner than re-aggregating the raw table at two levels in the same GROUP BY, and it gives each layer a clear, readable name.

Practice Grouping by Date Periods in SQL

Practice · easy ecommerce · Brightlane

Scenario: Brightlane's finance team needs a monthly revenue summary to track order activity over time.

Task: Write a query to return each calendar month, the number of orders placed in that month, and the total orders revenue for that month.

Assumptions:

  • The orders table holds one row per placed order, with the placement timestamp stored in ordered_at and the order amount stored in total_amount.
  • A calendar month is identified by its first day and covers every order placed within that month.

Output:

  • One row per calendar month present in the data.
  • Columns in this order: month (the first day of the calendar month), order_count, revenue.
Schema · ecommerce5 tables? = nullable
categories
idinteger
nametext
parent_id?integer
products
idinteger
nametext
category_id?integer
pricenumeric
stock_qtyinteger
attributes?jsonb
order_items
idinteger
order_id?integer
product_id?integer
quantityinteger
unit_pricenumeric
customers
idinteger
nametext
emailtext
city?text
countrytext
created_attimestamptz
is_activeboolean
orders
idinteger
customer_id?integer
ordered_attimestamptz
statustext
total_amountnumeric

Run previews · Check grades

Write a query, then run it to see results here.

Worked solution

The full breakdown walks through the shape, each clause, why this approach beats the alternatives, and the trap to avoid.

See the full worked solution

10 Grouping by Date Periods practice problems

Start learning to practice all 10 Grouping by Date Periods problems, with instant grading and mastery tracking.

Deeper guides on Grouping by Date Periods

Common questions about Grouping by Date Periods

Should I group by DATE_TRUNC or by EXTRACT(MONTH)?

DATE_TRUNC, almost always. EXTRACT(MONTH) returns 1 for every January in every year, so January 2024 and January 2025 collapse into a single group. DATE_TRUNC keeps the year, so each month of each year stays its own group and sorts in time order.

Why are my months sorting in the wrong order?

Usually because the query groups or sorts on a formatted string such as to_char(order_date, 'Mon YYYY'). Strings sort alphabetically, so April comes before February and February before January. Group and order on the DATE_TRUNC value, and format it only in the final SELECT.

Can I GROUP BY a column alias in PostgreSQL?

Yes. PostgreSQL accepts the alias from the SELECT list, or its position such as GROUP BY 1, so you do not have to repeat a long DATE_TRUNC expression in the GROUP BY clause.

How do I include days or months that have no rows?

GROUP BY cannot create them, because it only groups rows that already exist. Join your data to a generated list of every period, known as a date spine, so that each period is present whether or not anything happened in it.

How you actually get good at SQL

Reading explains SQL. Writing it, over and over with instant feedback, is what makes you fluent.

That's the whole SQLMaxx loop: 600+ real problems, instant AI feedback, mastery you can actually see, and spaced review that won't let you forget.

A stack of SQL practice problem cards, the top card showing an employees table.
615 problems · 66 concepts

Real problems. Not toy examples.

615 hand-built problems spanning all 66 concepts, from basic SELECTs to window functions, built on real schemas and real business questions, the kind you'll actually get asked on the job. Enough reps to make SQL automatic.

A retro computer showing a SQL query marked correct with a green checkmark.
Instant AI feedback

Write a query. Know if it's right in one second.

No copying an answer and hoping it clicked. The AI grader checks your real query against real data, catches exactly what's wrong, and explains the fix in plain English, like a senior analyst reading over your shoulder on every problem.

A circular mastery progress dial filling from blue to green, the SQLMaxx diamond at its center.
Mastery tracking

Stop guessing whether you actually know it.

SQLMaxx tracks every concept and shows you what you've mastered and what's still shaky. Your skills fill in one concept at a time, so 'I think I get joins' becomes something you can prove.

A SQL query editor circled by a blue return arrow with a clock, scheduled to come back for review.
Spaced review

Learn it once. Keep it for good.

Most of what you learn this week fades by next week. So when a concept comes due for review, SQLMaxx hands you a fresh problem to solve from a blank editor, not a flashcard to re-read. A research-backed spaced-repetition algorithm (FSRS) times each return for right before you'd forget, so your SQL is still there months later, when the interview or the job actually needs it.