Q:

Write a mysql statement to determine the age of each of the students

belongs to collection: MySQL Exercises

0

MySQL Timestampdiff Exercise

Write a mysql statement to determine the age of each of the students

Suppose the table is -
+----+--------------+------------+------------+
| id | name         | department | birth      |
+----+--------------+------------+------------+
|  1 | Maria Gloria | CS         | 1994-03-12 |
|  2 | John Smith   | IT         | 1993-02-07 |
|  3 | Gal Rao      | CS         | 1992-09-11 |
|  4 | Jakey Smith  | EC         | 1990-08-31 |
|  5 | Rama Saho    | IT         | 1994-12-09 |
|  6 | Maria Gaga   | EC         | 1993-10-09 |
+----+--------------+------------+------------+

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

To determine how many years old each of the student is, use the TIMESTAMPDIFF() function. It returns a value after subtracting a datetime expression from another. Both datetime or date expressions are required parameters.

The following query shows student data and age of each students in years.

 mysql> SELECT *,
    -> TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age
    -> FROM students;

Output of the above statement -

+----+--------------+------------+------------+------+
| id | name         | department | birth      | age  |
+----+--------------+------------+------------+------+
|  1 | Maria Gloria | CS         | 1994-03-12 |   24 |
|  2 | John Smith   | IT         | 1993-02-07 |   25 |
|  3 | Gal Rao      | CS         | 1992-09-11 |   25 |
|  4 | Jakey Smith  | EC         | 1990-08-31 |   27 |
|  5 | Rama Saho    | IT         | 1994-12-09 |   23 |
|  6 | Maria Gaga   | EC         | 1993-10-09 |   24 |
+----+--------------+------------+------------+------+

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

MySQL Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a mysql statement to retrieve name beginning... >>
<< Write a MySQL statement to select data of all depa...