sqlstreak

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

monthsignups
2026-0180
2026-02100
2026-03120

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 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. 2
    GROUP BY 1

    Group by the first selected column, the month label.

  3. 3
    ORDER BY 1

    YYYY-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

monthplaysminutes
2026-0127412626
2026-0263529230
2026-03101548315
2026-0448322195

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

weekplays
2025-12-293
2026-01-0530
2026-01-1276
2026-01-1986
2026-01-26100

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

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.

Start free