# Date difference in SQL: PostgreSQL and SQLite

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


**Short answer:** PostgreSQL subtracts dates directly: `d2 - d1` is a number of days. SQLite turns dates into day numbers first: `julianday(d2) - julianday(d1)`. Neither has `DATEDIFF`, which belongs to MySQL and SQL Server.

**The task:** Find how many days (or months) lie between two dates.

### Days from joining to the first order

PostgreSQL:

```sql
SELECT c.name, c.joined_on, MIN(o.ordered_on) AS first_order,
       MIN(o.ordered_on) - c.joined_on AS days_to_order
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days_to_order;
```

SQLite:

```sql
SELECT c.name, c.joined_on, MIN(o.ordered_on) AS first_order,
       CAST(julianday(MIN(o.ordered_on)) - julianday(c.joined_on) AS INTEGER) AS days_to_order
FROM customers c
JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.joined_on
ORDER BY days_to_order;
```

Result (both engines):

| name | joined_on | first_order | days_to_order |
| --- | --- | --- | --- |
| Marcus Bell | 2025-10-30 | 2026-03-14 | 135 |
| Lena Fischer | 2025-09-09 | 2026-03-11 | 183 |
| Sam Okafor | 2025-07-21 | 2026-03-08 | 230 |
| Ellie Park | 2025-06-15 | 2026-03-05 | 263 |
| Tomás Herrera | 2025-05-02 | 2026-03-05 | 307 |
| Priya Nair | 2025-03-19 | 2026-03-04 | 350 |
| Jonah Reed | 2025-03-04 | 2026-03-01 | 362 |
| Maya Ortiz | 2025-02-11 | 2026-03-01 | 383 |

### Whole months between two dates

Count calendar months, minus one when the day of the month hasn't come round yet.

PostgreSQL:

```sql
SELECT name, hired_on,
  EXTRACT(YEAR FROM age(DATE '2026-11-22', hired_on)) * 12
  + EXTRACT(MONTH FROM age(DATE '2026-11-22', hired_on)) AS months_employed
FROM staff
ORDER BY hired_on;
```

SQLite:

```sql
SELECT name, hired_on,
  (strftime('%Y', '2026-11-22') - strftime('%Y', hired_on)) * 12
  + (strftime('%m', '2026-11-22') - strftime('%m', hired_on))
  - (strftime('%d', '2026-11-22') < strftime('%d', hired_on)) AS months_employed
FROM staff
ORDER BY hired_on;
```

Result (both engines):

| name | hired_on | months_employed |
| --- | --- | --- |
| Rosa Vega | 2024-01-10 | 34 |
| Omar Haddad | 2024-03-01 | 32 |
| Ben Carter | 2024-04-20 | 31 |
| Lia Moreau | 2024-06-15 | 29 |
| Ivan Petrov | 2024-08-01 | 27 |
| Yuki Tanaka | 2025-02-01 | 21 |
| Zara Ali | 2025-05-12 | 18 |
| Noah Kim | 2025-09-30 | 13 |

### DATEDIFF fails in both

```sql
SELECT DATEDIFF('2026-03-31', ordered_on) FROM orders;
```

PostgreSQL: error: function datediff(unknown, date) does not exist

SQLite: error: no such function: DATEDIFF

## How it works

- `MIN(o.ordered_on) - c.joined_on`: PostgreSQL: date minus date is a whole number of days.
- `julianday(…) - julianday(…)`: SQLite: julianday turns a date into a day number; CAST(… AS INTEGER) drops the fraction.

## Variations

### Difference from today

Use `CURRENT_DATE - d` in PostgreSQL and `julianday('now') - julianday(d)` in SQLite. The examples use a fixed date so the results on this page don't change daily.

### Hours or minutes between timestamps

PostgreSQL: `EXTRACT(EPOCH FROM t2 - t1) / 3600`. SQLite: `(julianday(t2) - julianday(t1)) * 24`.

## Common mistakes

- Subtracting date text in SQLite: `'2026-03-31' - '2026-03-01'` subtracts the leading numbers (2026 - 2026) and gives 0.
- Dividing days by 30 to get months: months have 28 to 31 days. Count calendar months instead.
- Using DATEDIFF, DATE_DIFF or TIMESTAMPDIFF: those are other databases' functions.

## PostgreSQL and SQLite

This is the area where the two differ most: date arithmetic and `age()` in PostgreSQL, `julianday` and `strftime` in SQLite. Every example shows both forms, and each pair returned the same numbers.
