# How to calculate age from a birth date in SQL

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


**Short answer:** In PostgreSQL: `EXTRACT(YEAR FROM age(today, born))`. In SQLite: subtract the years, then take 1 off if this year's birthday hasn't happened yet: `(strftime('%Y', today) - strftime('%Y', born)) - (strftime('%m-%d', today) < strftime('%m-%d', born))`.

**The task:** Work out someone's age in whole years from their date of birth.

### Ages on 22 November 2026

A small inline list of people; one birthday falls the day after, one on the day itself.

PostgreSQL:

```sql
WITH people(name, born) AS (
  VALUES ('Ada', DATE '1990-11-23'), ('Ben', DATE '1990-11-22'),
         ('Cleo', DATE '2004-02-29'), ('Dev', DATE '1985-06-01')
)
SELECT name, born,
       EXTRACT(YEAR FROM age(DATE '2026-11-22', born))::int AS age
FROM people
ORDER BY name;
```

SQLite:

```sql
WITH people(name, born) AS (
  VALUES ('Ada', '1990-11-23'), ('Ben', '1990-11-22'),
         ('Cleo', '2004-02-29'), ('Dev', '1985-06-01')
)
SELECT name, born,
       (strftime('%Y', '2026-11-22') - strftime('%Y', born))
       - (strftime('%m-%d', '2026-11-22') < strftime('%m-%d', born)) AS age
FROM people
ORDER BY name;
```

Result (both engines):

| name | born | age |
| --- | --- | --- |
| Ada | 1990-11-23 | 35 |
| Ben | 1990-11-22 | 36 |
| Cleo | 2004-02-29 | 22 |
| Dev | 1985-06-01 | 41 |

### Years of service from a hire date

The same calculation on real data: whole years each staff member has worked, on 22 November 2026.

PostgreSQL:

```sql
SELECT name, hired_on,
       EXTRACT(YEAR FROM age(DATE '2026-11-22', hired_on))::int AS years
FROM staff
ORDER BY hired_on;
```

SQLite:

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

Result (both engines):

| name | hired_on | years |
| --- | --- | --- |
| Rosa Vega | 2024-01-10 | 2 |
| Omar Haddad | 2024-03-01 | 2 |
| Ben Carter | 2024-04-20 | 2 |
| Lia Moreau | 2024-06-15 | 2 |
| Ivan Petrov | 2024-08-01 | 2 |
| Yuki Tanaka | 2025-02-01 | 1 |
| Zara Ali | 2025-05-12 | 1 |
| Noah Kim | 2025-09-30 | 1 |

## How it works

- `age(DATE '2026-11-22', born)`: PostgreSQL: the gap as years, months and days.
- `EXTRACT(YEAR FROM …)`: Keep the whole years.
- `strftime('%m-%d', today) < strftime('%m-%d', born)`: SQLite: true (1) if the birthday is still to come this year; subtract it.

## Variations

### Age today

Swap the fixed date for `CURRENT_DATE` (PostgreSQL) or `'now'` (SQLite). The page uses a fixed date so its results stay checkable.

## Common mistakes

- Days divided by 365: leap years make it wrong around birthdays.
- Only subtracting the years: everyone born later in the year comes out one year too old.
- Storing birth dates as text in another format (`17/05/1990`): convert to ISO first, or the comparisons fail.

## PostgreSQL and SQLite

PostgreSQL has `age()`; SQLite doesn't, so it compares month-day text. Both examples returned the same ages on both engines, including the leap-day birthday.
