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.
💻 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.