How to Calculate Age in SQL (PostgreSQL, MySQL, SQL Server)
Learn practical SQL patterns to calculate age from a date of birth: years completed, exact years and months, and age as of a specific date. Includes examples for PostgreSQL, MySQL, and SQL Server.
Need to calculate age in SQL from a date of birth? This guide shows practical, copy ready queries for common databases. You will learn how to compute:
- Age in years (years completed)
- Age as of a specific date (not just today)
- An exact style output (years and months) when you need more detail
1) Decide what "age" means for your use case
Most business logic uses years completed. That means someone born on 2000-06-10 is 25 on 2026-06-10, not earlier. Be careful with simple year subtraction like YEAR(today) - YEAR(dob), because it is wrong before the birthday.
2) PostgreSQL: age in years (years completed)
PostgreSQL has a built in age() function that returns an interval. Extract full years like this:
SELECT EXTRACT(YEAR FROM age(CURRENT_DATE, dob)) AS age_years
FROM people;
PostgreSQL: age as of a specific date
SELECT EXTRACT(YEAR FROM age(DATE '2026-02-23', dob)) AS age_years
FROM people;
3) MySQL: age in years (years completed)
In MySQL, use TIMESTAMPDIFF:
SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age_years
FROM people;
MySQL: age as of a specific date
SELECT TIMESTAMPDIFF(YEAR, dob, DATE('2026-02-23')) AS age_years
FROM people;
4) SQL Server: age in years (years completed)
A common pattern in SQL Server is DATEDIFF minus a birthday check:
SELECT
DATEDIFF(YEAR, dob, CAST(GETDATE() AS date))
- CASE
WHEN DATEADD(YEAR, DATEDIFF(YEAR, dob, CAST(GETDATE() AS date)), dob) > CAST(GETDATE() AS date)
THEN 1 ELSE 0
END AS age_years
FROM people;
SQL Server: age as of a specific date
DECLARE @as_of date = '2026-02-23';
SELECT
DATEDIFF(YEAR, dob, @as_of)
- CASE
WHEN DATEADD(YEAR, DATEDIFF(YEAR, dob, @as_of), dob) > @as_of
THEN 1 ELSE 0
END AS age_years
FROM people;
5) Exact age (years and months) example
If you want an "exact" presentation like "31 years 4 months", compute years first, then months after the last birthday. Here is one PostgreSQL example:
WITH base AS (
SELECT dob, CURRENT_DATE AS as_of
FROM people
)
SELECT
EXTRACT(YEAR FROM age(as_of, dob)) AS years,
EXTRACT(MONTH FROM age(as_of, dob)) AS months
FROM base;
Common pitfalls
- Time zones: store DOB as a
datewhen possible. Datetimes can shift across zones. - Leap day birthdays: define what happens on non leap years. Most systems treat 2004-02-29 birthdays as 02-28 or 03-01 depending on policy.
- Null or future DOB: filter or guard, otherwise your query will return nulls or negative values.
Quick sanity check
If you want to verify a single DOB quickly, you can use the online calculator: Calculate Age Online.
Related guides
- How to Calculate Age (complete guide)
- How to Calculate Age in Excel
- How to Calculate Age in JavaScript
FAQ
Why is YEAR(today) minus YEAR(dob) wrong?
It ignores whether the birthday has happened yet this year, so it overstates age before the birthday.
What is the safest SQL type for date of birth?
Use a date column (not datetime) for DOB if you only care about the calendar day.