runtime
32 / 88

lesson 32 Dates, strings & subqueries · intermediate

Dates & monthly analysis

Group events into months and analyze dates.

the idea

SQLite stores these dates as ISO text. strftime formats dates: %Y-%m extracts a year and month. Group by that expression to build a monthly summary. Other SQL dialects use different date functions.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

orders6 columns · 12 rows
idinteger · keycustomer_idintegerproduct_idintegerquantityintegerstatustextordered_attext
101112paid2025-01-01
102231paid2025-01-02
103121paid2025-01-05
104352pending2025-01-08
105445paid2025-02-01

showing 5 of 12 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT strftime('%Y-%m', ordered_at) AS month, COUNT(*) AS order_count
FROM orders
GROUP BY month;

Watch for: Grouping only by month number mixes years.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Read the order dates. Each order has one timestamp. The next step groups those timestamps into year-month buckets.

    step-1.sql
    SELECT ordered_at
    FROM orders;
  2. Run the reference. SQLite stores these dates as ISO text. strftime formats dates: %Y-%m extracts a year and month. Group by that expression to build a monthly summary. Other SQL dialects use different date functions.

    step-2.sql
    SELECT strftime('%Y-%m', ordered_at) AS month, COUNT(*) AS order_count
    FROM orders
    GROUP BY month;

challenge 1

Return month (YYYY-MM) and order_count for all orders by month.

Return monthorder_countany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.