← All lessons
Combining and summarizing · Lesson 9 of 10

Dates and times

Every database stores dates, but how differs. SQLite has no date type: it keeps dates as text in the year-month-day form and gives you functions that understand that text. PostgreSQL, MySQL and the others have real DATE and TIMESTAMP types. The ideas are the same; the function names change, and the lesson shows the common ones.

To report by month, cut each date down to its month. In SQLite, strftime('%Y-%m', d) formats a date as year and month (%Y is the year, %m the month, %d the day):

SELECT strftime('%Y-%m', ordered_on) AS month, COUNT(*) AS orders
FROM orders
GROUP BY month
ORDER BY month;
Returns
month   | orders
--------+-------
2024-09 | 6
2024-10 | 4
(2 rows)

In PostgreSQL that's DATE_TRUNC('month', d), and EXTRACT(YEAR FROM d) takes out one part; SQL Server has DATEPART and FORMAT.

date(d, '+7 days') moves a date. You can add or take away days, months or years: '-1 month', '+1 year'. Elsewhere it's d + INTERVAL '7 days' (PostgreSQL) or DATEADD(day, 7, d) (SQL Server).

SELECT id, ordered_on, date(ordered_on, '+7 days') AS due
FROM orders
WHERE customer_id = 2;
Returns
id  | ordered_on | due
----+------------+-----------
102 | 2024-09-03 | 2024-09-10
106 | 2024-09-20 | 2024-09-27
(2 rows)

For the gap between two dates, SQLite's julianday(d) turns a date into a count of days, so subtracting two gives the days between them. (PostgreSQL subtracts dates directly; SQL Server has DATEDIFF.)

SELECT name, joined, julianday('2024-10-31') - julianday(joined) AS days_as_customer
FROM customers
WHERE city = 'Austin';
Returns
name | joined     | days_as_customer
-----+------------+-----------------
Ana  | 2024-01-15 | 290
Cleo | 2024-03-22 | 223
Hana | 2024-09-12 | 49
(3 rows)

To pick out one month, use a range on the raw column: ordered_on >= '2024-09-01' AND ordered_on < '2024-10-01'. "From the first, up to but not including the next first" works for every month length, and later you'll see it's also the version a database can speed up with an index. date('now') gives today's date, which is handy in real reports but changes every day, so the practice doesn't use it.

Practice

How many orders were placed in each month: the month as YYYY-MM and the count.

The tables
  • customers (id, name, city, joined)
  • products (id, name, category, price)
  • orders (id, customer_id, ordered_on, status)
  • order_items (order_id, product_id, qty)
  • signups (id, email, name, signed_up_at)
  • payments (id, order_id, amount, currency, paid_at)
  • events (id, received_at, payload (JSON text))
  • staff (id, name, role, manager_id, salary, hired)
  • logins (customer_id, day)
More info

Quick reference

strftime('%Y-%m', d)SQLite: format a date (year-month). DATE_TRUNC in PostgreSQL.
date(d, '+7 days')SQLite: move a date. d + INTERVAL / DATEADD elsewhere.
julianday(a) - julianday(b)SQLite: days between. DATEDIFF / a - b elsewhere.
>= first AND < next firstThe safe way to pick out a month or a day.
date('now')Today's date (changes every day).

The other AI and data engineering courses