Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 146

Calculate Age from DOB in MySQL

In this lesson: Get exact age from DOB in MySQL using TIMESTAMPDIFF and CURDATE — years, months, days — with output. In Hindi & English.

Age from a birth date

A very common need is turning a stored date of birth into an age in years. It sounds simple, but doing it wrong gives ages that are off by one for much of the year. MySQL's TIMESTAMPDIFF does it correctly and easily.

💡 Use TIMESTAMPDIFF(YEAR, dob, CURDATE()) — it counts full years, respecting the birthday.

Why year subtraction fails

-- WRONG
SELECT YEAR(CURDATE()) - YEAR('2000-12-25');   -- gives 26 in 2026
-- but the birthday on Dec 25 has not happened yet!

Subtracting years ignores the month and day. Someone born in December is not yet a year older until their birthday. This method is wrong for most of the year.

SELECT name, dob,
    TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM students;
+------+------------+-----+ | name | dob | age | +------+------------+-----+ | Aman | 2005-03-10 | 21 | | Diya | 2008-11-20 | 17 | +------+------------+-----+

TIMESTAMPDIFF(YEAR, dob, CURDATE()) counts complete years between the birth date and today, correctly handling whether this year's birthday has passed.

Using CURDATE()

CURDATE() returns today's date, so the age is always current — it updates automatically each day without you changing anything. You could also compare against a fixed date by replacing CURDATE() with a specific date, for example to find age as of an exam date.

Filtering by age

SELECT name
FROM members
WHERE TIMESTAMPDIFF(YEAR, dob, CURDATE()) >= 18;

Put the age calculation in the WHERE clause to filter. This returns all members who are 18 or older, computing each one's age from their date of birth on the fly.

Age in months or days

SELECT name,
    TIMESTAMPDIFF(MONTH, dob, CURDATE()) AS age_months,
    TIMESTAMPDIFF(DAY, dob, CURDATE()) AS age_days
FROM babies;

Change the first argument to MONTH or DAY for finer detail. This is handy for infants, memberships, or any case where whole years are too coarse.

Common mistakes

  • Subtracting years only. It ignores month and day, giving wrong ages.
  • Getting the argument order wrong. It is (unit, start, end) — dob then CURDATE().
  • Storing dob as text. Use a DATE column so date functions work.
  • Forgetting CURDATE() updates daily. That is a feature — age stays current.

Summary

  • Calculate age with TIMESTAMPDIFF(YEAR, dob, CURDATE()).
  • Plain year subtraction is wrong — it ignores month and day.
  • CURDATE() keeps the age current automatically.
  • Filter by age by putting the calculation in WHERE.
  • Use MONTH or DAY units for finer detail.

Next, find month boundaries in first and last day of a month, or recent rows in records from the last 7 days.

Frequently Asked Questions

How do I calculate age from a date of birth in MySQL?
Use TIMESTAMPDIFF(YEAR, dob, CURDATE()). It correctly counts the number of full years between the birth date and today, accounting for whether this year's birthday has passed. This is the accurate way to get age.
Why is subtracting years wrong for age?
Simply doing YEAR(CURDATE()) - YEAR(dob) ignores the month and day. Someone born in December would wrongly appear one year older for most of the year, because their birthday has not happened yet. TIMESTAMPDIFF avoids this.
What does TIMESTAMPDIFF do?
TIMESTAMPDIFF returns the difference between two dates in a chosen unit — YEAR, MONTH, DAY and more. For age, TIMESTAMPDIFF(YEAR, dob, CURDATE()) gives full years completed, which is exactly age.
How do I find people above a certain age?
Put the age calculation in a WHERE clause: WHERE TIMESTAMPDIFF(YEAR, dob, CURDATE()) >= 18. This returns everyone who is at least 18, computing each person's age from their date of birth.
How do I get age in months or days?
Change the unit in TIMESTAMPDIFF: use MONTH for months (TIMESTAMPDIFF(MONTH, dob, CURDATE())) or DAY for days. This is useful for infants or precise durations rather than whole years.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicFirst and Last Day of Month Your progress: 146 / 151 (97%) · Lesson 147 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free