MySQL + SQL · Lesson 1
Age From Dob
Age from DOB
To calculate exact age from a date of birth, MySQL has the TIMESTAMPDIFF function.
Using TIMESTAMPDIFF
SELECT name, dob,
TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM students;name dob age
---- ---------- ---
Aman 2005-03-15 21
How it Works
TIMESTAMPDIFF(YEAR, dob, CURDATE()) counts the full years between the birth date and today, giving the exact age.
Summary
- Use
TIMESTAMPDIFF(YEAR, dob, CURDATE())for exact age in years. - CURDATE() gives today's date.