# How to group by month in SQL (PostgreSQL and SQLite)

Source: https://sqlstreak.com/sql/how-to/group-by-month · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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:

```sql
SELECT to_char(signed_up_on, 'YYYY-MM') AS month,
       COUNT(*) AS signups
FROM users
GROUP BY 1
ORDER BY 1;
```

SQLite:

```sql
SELECT strftime('%Y-%m', signed_up_on) AS month,
       COUNT(*) AS signups
FROM users
GROUP BY 1
ORDER BY 1;
```

Result (both engines):

| month | signups |
| --- | --- |
| 2026-01 | 80 |
| 2026-02 | 100 |
| 2026-03 | 120 |

### 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.

```sql
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 (both engines):

| month | plays | minutes |
| --- | --- | --- |
| 2026-01 | 274 | 12626 |
| 2026-02 | 635 | 29230 |
| 2026-03 | 1015 | 48315 |
| 2026-04 | 483 | 22195 |

### Per week, starting on Monday

PostgreSQL truncates to the week; SQLite moves each date back to its Monday.

PostgreSQL:

```sql
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:

```sql
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 (both engines):

| week | plays |
| --- | --- |
| 2025-12-29 | 3 |
| 2026-01-05 | 30 |
| 2026-01-12 | 76 |
| 2026-01-19 | 86 |
| 2026-01-26 | 100 |

## How it works

- `to_char(signed_up_on, 'YYYY-MM')`: PostgreSQL: the date as text like 2026-03. In SQLite, strftime('%Y-%m', …) gives the same text.
- `GROUP BY 1`: Group by the first selected column, the month label.
- `ORDER BY 1`: YYYY-MM labels sort in calendar order.

## 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.
