SQL how-to
How to calculate age from a birth date in SQL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
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
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
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 (PostgreSQL and SQLite, identical): 4 rows
| name | born | age |
|---|---|---|
| Ada | 1990-11-23 | 35 |
| Ben | 1990-11-22 | 36 |
| Cleo | 2004-02-29 | 22 |
| Dev | 1985-06-01 | 41 |
Try it: PostgreSQL playground · SQLite playground
How it works
- 1
age(DATE '2026-11-22', born)PostgreSQL: the gap as years, months and days.
- 2
EXTRACT(YEAR FROM …)Keep the whole years.
- 3
strftime('%m-%d', today) < strftime('%m-%d', born)SQLite: true (1) if the birthday is still to come this year; subtract it.
More examples
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
SELECT name, hired_on,
EXTRACT(YEAR FROM age(DATE '2026-11-22', hired_on))::int AS years
FROM staff
ORDER BY hired_on;SQLite
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 (PostgreSQL and SQLite, identical): 8 rows
| 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 |
Try it: PostgreSQL playground · SQLite playground
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.
Official documentation
Go deeper
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Date maths (Pro)
Make it stick in five minutes a day
“Date maths” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.