Recipe · SQL recipes

Grouping by month in SQL: dates and GROUP BY

To summarise by month, group by a key that keeps both the year and the month; then apply COUNT or SUM.

Before continuing: This entry is part of the GROUP BY guide. If you are new to the topic, start with GROUP BY.

Query

The teaching database contains 18 orders placed in May and June 2025. In SQLite we can convert date to the format YYYY-MM with strftime() and group by that value.

SQLite · run on the teaching database
SELECT strftime('%Y-%m', date) AS month,
       COUNT(*) AS orders,
       ROUND(SUM(total), 2) AS billing
FROM orders
GROUP BY strftime('%Y-%m', date)
ORDER BY month;

The query produces one row per month. COUNT(*) counts the orders and SUM(total) adds up their amounts. The ORDER BY month sorts chronologically because the key is a four-digit year followed by a two-digit month.

How it works

month_key(date) is not a real SQL function: it stands for the operation that varies from dialect to dialect. What matters is that the same expression consistently identifies each month.

Avoid this confusion

Grouping by month number alone

-- Incorrect when multiple years are present
GROUP BY EXTRACT(MONTH FROM date)

This expression lumps January 2025 together with January 2026, for example. Include the year in the key, or use a function that truncates the date to the whole month.

Formatting a nice label before getting the key right

The display format and the grouping key are not quite the same problem. First make sure each period is unambiguous and sorts chronologically; then decide whether to show 2025-06, june 2025 or some other label.

Ignoring the time zone of timestamps

If you store points in time with a time zone, a sale near midnight can fall in different months depending on the zone you read it in. Decide on the business time zone before truncating or formatting the timestamp.

Key differences between engines

PostgreSQL: date_trunc

PostgreSQL can truncate a timestamp to a given precision. With date_trunc('month', date), every date in a month becomes the first day of that month, which makes a natural date-based key.

PostgreSQL
SELECT date_trunc('month', date) AS month,
       COUNT(*) AS orders,
       SUM(total) AS billing
FROM orders
GROUP BY date_trunc('month', date)
ORDER BY month;

MySQL: DATE_FORMAT

MySQL can build a text key YYYY-MM with DATE_FORMAT. It also offers EXTRACT to get the year and month separately. Keep both parts if the data covers more than one year.

MySQL 8.4
SELECT DATE_FORMAT(date, '%Y-%m') AS month,
       COUNT(*) AS orders,
       SUM(total) AS billing
FROM orders
GROUP BY DATE_FORMAT(date, '%Y-%m')
ORDER BY month;

SQL Server 2022+: DATETRUNC

SQL Server 2022 introduced DATETRUNC. DATETRUNC(month, date) returns the date truncated to the start of the month. In earlier versions you can group by YEAR(date) and MONTH(date) as two separate keys.

SQL Server 2022+
SELECT DATETRUNC(month, date) AS month,
       COUNT(*) AS orders,
       SUM(total) AS billing
FROM orders
GROUP BY DATETRUNC(month, date)
ORDER BY month;

SQLite: strftime

SQLite has no dedicated date/time type and no date_trunc. For ISO dates like the ones in the teaching database, strftime('%Y-%m', date) builds a readable monthly key that sorts correctly.