sqlstreak

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

namebornage
Ada1990-11-2335
Ben1990-11-2236
Cleo2004-02-2922
Dev1985-06-0141

Try it: PostgreSQL playground · SQLite playground

How it works

  1. 1
    age(DATE '2026-11-22', born)

    PostgreSQL: the gap as years, months and days.

  2. 2
    EXTRACT(YEAR FROM …)

    Keep the whole years.

  3. 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

namehired_onyears
Rosa Vega2024-01-102
Omar Haddad2024-03-012
Ben Carter2024-04-202
Lia Moreau2024-06-152
Ivan Petrov2024-08-012
Yuki Tanaka2025-02-011
Zara Ali2025-05-121
Noah Kim2025-09-301

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

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.

Start free