Querying age from DOB in MySQL?


Let us first create a table −

mysql> create table DemoTable611 (DOB date);
Query OK, 0 rows affected (0.99 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable611 values('1996-04-21');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable611 values('2001-01-31');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable611 values('2004-12-21');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable611 values('2000-02-03');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable611;

This will produce the following output −

+------------+
| DOB        |
+------------+
| 1996-04-21 |
| 2001-01-31 |
| 2004-12-21 |
| 2000-02-03 |
+------------+
4 rows in set (0.00 sec)

Querying age from DOB −

mysql> select *from DemoTable611 where DOB >= (CURDATE() - INTERVAL 16 YEAR);

This will produce the following output −

+------------+
| DOB        |
+------------+
| 2004-12-21 |
+------------+
1 row in set (0.00 sec)

Updated on: 22-Aug-2019

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements