Technical & Developer Tools
SQL Age Query Generator
Generate age calculation SQL queries for PostgreSQL, MySQL, and SQLite.
The code returns for the sample
- Generated code
Step-by-step working
- Sample: 2000-03-10 → 2026-11-29
- Expected result: 26
What Is the SQL Age Query Generator?
A SQL Age Query Generator is a tool that writes Structured Query Language (SQL) queries that calculate age from a birth date column in PostgreSQL, MySQL, SQLite and SQL Server.
Each database has its own date functions. PostgreSQL has AGE(), MySQL has TIMESTAMPDIFF(), SQL Server has DATEDIFF() with a birthday correction, and SQLite compares formatted strings.
A naive year subtraction, such as YEAR(NOW()) − YEAR(birth_date), is wrong for every person whose birthday has not yet come this year. The generated queries handle that case.
How Does the SQL Age Query Generator Work?
The SQL Age Query Generator works by filling the table and column names into a tested age expression for the chosen database.
- Pick the database: Choose PostgreSQL, MySQL, SQLite or SQL Server.
- Enter the table and column: For example, table people and column birth_date.
- Choose the reference date: Use the current date or a fixed date literal.
- Copy the query: Run the SELECT or add the expression to a view.
SQL Age Query Generator Formula
The MySQL age query is SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM people;, which returns completed years.
PostgreSQL: SELECT EXTRACT(YEAR FROM AGE(CURRENT_DATE, birth_date))::int AS age FROM people;
MySQL: SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) AS age FROM people;SQLite: SELECT (strftime('%Y','now') - strftime('%Y', birth_date)) - (strftime('%m-%d','now') < strftime('%m-%d', birth_date)) AS age FROM people;SQL Server: SELECT DATEDIFF(YEAR, birth_date, GETDATE()) - CASE WHEN DATEADD(YEAR, DATEDIFF(YEAR, birth_date, GETDATE()), birth_date) > GETDATE() THEN 1 ELSE 0 END AS age FROM people;
| Symbol | Meaning |
|---|---|
AGE(a, b) | PostgreSQL interval from b to a, such as 26 years 8 mons 15 days |
TIMESTAMPDIFF(YEAR, …) | MySQL count of whole years |
DATEDIFF(YEAR, …) | SQL Server count of year boundaries crossed, which needs a correction |
SQL Age Query Generator Example
Compute the age for birth_date = '2000-03-10' on 2026-11-25 in PostgreSQL.
SELECT AGE(DATE '2026-11-25', DATE '2000-03-10');- Returns: 26 years 8 mons 15 days
The query returns 26 years 8 mons 15 days, and EXTRACT(YEAR …) gives 26.
SQL Age Query Generator Conversion Table
The table compares 5 SQL age expressions and their results for a birth date of 2000-03-10 and a reference date of 2026-11-25:
| Database | Expression | Result |
|---|---|---|
| PostgreSQL | AGE(DATE '2026-11-25', birth_date) | 26 years 8 mons 15 days |
| PostgreSQL | DATE '2026-11-25' - birth_date | 9,756 (days) |
| MySQL | TIMESTAMPDIFF(YEAR, birth_date, '2026-11-25') | 26 |
| SQL Server | DATEDIFF(YEAR, birth_date, '2026-11-25') (uncorrected) | 26 (boundaries crossed) |
| SQLite | julianday('2026-11-25') - julianday(birth_date) | 9,756.0 (days) |
SQL Server DATEDIFF(YEAR, …) counts year boundaries, so a person born on December 31 gains 1 year on January 1. Use the corrected query.
What Are the Uses of the SQL Age Query Generator?
The SQL Age Query Generator has 4 main uses: reports, filters, views and data checks.
- Reports: Analysts group customers by age band in SQL.
- Filters: Queries select users aged 18 or over before sending content.
- Views: DBAs add a computed age column to a view.
- Data checks: Queries find birth dates in the future or ages over 122.
SQL Age Query Generator FAQ
Why not store age in a column?
Stored ages go stale after each birthday. Store the birth date and compute age in the query.
How do I filter adults in MySQL?
Filter adults in MySQL with WHERE birth_date <= CURDATE() - INTERVAL 18 YEAR, which uses an index on birth_date.
Written and reviewed by Iqbal Hussain. Last reviewed. Calendar rules and sources: methodology.
Related Technical & Developer Tools Tools
Time Zone Aware Age Calculator
Compare birth and reference timestamps across global time zones.
Unix Birth Timestamp Age Calculator
Calculate elapsed age from seconds-since-epoch Unix timestamps.
ISO 8601 Birth Date Age Calculator
Calculate age from standard ISO 8601 date-time strings.
Birth Date Validator
Check date validity, leap day legality, and future date errors.
Excel Age Formula Generator
Generate DATEDIF and YEARFRAC spreadsheet age formulas.
Google Sheets Age Formula Generator
Generate cloud spreadsheet formulas for birth-date age tracking.