Answer: The DATE Column and Calculations
Find a detailed explanation of how to calculate the age from the date using an SQL query.
Solution
The solution is given below:
Press + to interact
/* The query to calculate age of an employee */SELECT *, FLOOR(TIMESTAMPDIFF(DAY, DateOfBirth, NOW()) / 365) AS AgeFROM Employees;
Explanation
The explanation of the solution code is given below:
Line 2: The
SELECT
query selects all the columns using*
and the calculated column for age. TheTIMESTAMPDIFF()
function takes three variables, the unit of time (e.g.,YEAR
,MONTH
,DAY
), and twodatetime
expressions. It calculates the difference between thedatetime
expressions in the specified unit. TheFLOOR()
function rounds a numeric ...