Skip to main content
Calculate Age From Date Of Birth
Calculate Age From Date Of Birth
Exact Age Calculator

Technical & Developer Tools

SQL Age Query Generator

Generate age calculation SQL queries for PostgreSQL, MySQL, and SQLite.

SQL Age Query Generator Inputs

Return

The code returns for the sample26

Generated code
-- PostgreSQL SELECT EXTRACT(YEAR FROM AGE(CURRENT_DATE, birth_date))::int AS age FROM people; -- MySQL / MariaDB SELECT TIMESTAMPDIFF(YEAR, birth_date, CURDATE()) 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; -- SQLite SELECT (strftime('%Y','now') - strftime('%Y', birth_date)) - (strftime('%m-%d','now') < strftime('%m-%d', birth_date)) AS age FROM people;
Step-by-step working
  1. Sample: 2000-03-10 → 2026-11-29
  2. 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.

  1. Pick the database: Choose PostgreSQL, MySQL, SQLite or SQL Server.
  2. Enter the table and column: For example, table people and column birth_date.
  3. Choose the reference date: Use the current date or a fixed date literal.
  4. 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;
Variables in the SQL Age Query Generator formula
SymbolMeaning
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.

  1. SELECT AGE(DATE '2026-11-25', DATE '2000-03-10');
  2. 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:

Age expressions by database for birth_date = 2000-03-10 on 2026-11-25
DatabaseExpressionResult
PostgreSQLAGE(DATE '2026-11-25', birth_date)26 years 8 mons 15 days
PostgreSQLDATE '2026-11-25' - birth_date9,756 (days)
MySQLTIMESTAMPDIFF(YEAR, birth_date, '2026-11-25')26
SQL ServerDATEDIFF(YEAR, birth_date, '2026-11-25') (uncorrected)26 (boundaries crossed)
SQLitejulianday('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.