SQL how-to
How to group by month in SQL (PostgreSQL and SQLite)
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
Turn each date into its month and group by that: to_char(d, 'YYYY-MM') or date_trunc('month', d) in PostgreSQL, strftime('%Y-%m', d) in SQLite. Keep the year in the label so March 2025 and March 2026 don't merge.
The task: Count or add up rows per month, or per week or year.
Sign-ups per month
PostgreSQL
SELECT to_char(signed_up_on, 'YYYY-MM') AS month,
COUNT(*) AS signups
FROM users
GROUP BY 1
ORDER BY 1;SQLite
SELECT strftime('%Y-%m', signed_up_on) AS month,
COUNT(*) AS signups
FROM users
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 3 rows
| month | signups |
|---|---|
| 2026-01 | 80 |
| 2026-02 | 100 |
| 2026-03 | 120 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
to_char(signed_up_on, 'YYYY-MM')PostgreSQL: the date as text like 2026-03. In SQLite, strftime('%Y-%m', …) gives the same text.
- 2
GROUP BY 1Group by the first selected column, the month label.
- 3
ORDER BY 1YYYY-MM labels sort in calendar order.
More examples
One query for both: cut the text
Cast the date to ISO text and keep the first seven characters (YYYY-MM). It works the same in both databases.
SELECT SUBSTR(CAST(played_on AS TEXT), 1, 7) AS month,
COUNT(*) AS plays,
SUM(minutes) AS minutes
FROM plays
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 4 rows
| month | plays | minutes |
|---|---|---|
| 2026-01 | 274 | 12626 |
| 2026-02 | 635 | 29230 |
| 2026-03 | 1015 | 48315 |
| 2026-04 | 483 | 22195 |
Try it: PostgreSQL playground · SQLite playground
Per week, starting on Monday
PostgreSQL truncates to the week; SQLite moves each date back to its Monday.
PostgreSQL
SELECT date_trunc('week', played_on)::date AS week,
COUNT(*) AS plays
FROM plays
WHERE played_on < '2026-02-02'
GROUP BY 1
ORDER BY 1;SQLite
SELECT date(played_on, '-6 days', 'weekday 1') AS week,
COUNT(*) AS plays
FROM plays
WHERE played_on < '2026-02-02'
GROUP BY 1
ORDER BY 1;Result (PostgreSQL and SQLite, identical): 5 rows
| week | plays |
|---|---|
| 2025-12-29 | 3 |
| 2026-01-05 | 30 |
| 2026-01-12 | 76 |
| 2026-01-19 | 86 |
| 2026-01-26 | 100 |
Try it: PostgreSQL playground · SQLite playground
Variations
Months with no rows
Grouping only shows months that have data. To list empty months with 0, generate the months first (generate_series in PostgreSQL, a recursive CTE in SQLite) and LEFT JOIN the counts.
Per year
EXTRACT(YEAR FROM d) in PostgreSQL, strftime('%Y', d) in SQLite.
Common mistakes
Grouping by the month number alone (
EXTRACT(MONTH FROM d)): every March of every year lands in one group.Sorting by a month name: April comes before January alphabetically. Sort by the YYYY-MM label or the truncated date.
Timestamps near midnight: a time zone shift can move a row into the next day or month. Convert before grouping.
PostgreSQL and SQLite
This is where the two differ: PostgreSQL has to_char, date_trunc and EXTRACT; SQLite has strftime and date() modifiers. The text trick, SUBSTR(CAST(d AS TEXT), 1, 7), works in both because both print dates as ISO text.
Official documentation
Go deeper
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Working with dates (Pro)
Make it stick in five minutes a day
“Working with dates” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.