Calculate Age Differences Using AGE Function in PostgreSQL
📖 Scenario: You work in a human resources department. You have a table of employees with their birth dates and hire dates. You want to find out how many years, months, and days each employee has been working at the company compared to their age.
🎯 Goal: Create a PostgreSQL query that uses the AGE function to calculate the difference between two dates: the employee's hire date and birth date, and also between the current date and the birth date.
📋 What You'll Learn
Create a table called
employees with columns id (integer), name (text), birth_date (date), and hire_date (date).Insert three employees with exact birth and hire dates.
Write a SELECT query that uses the
AGE function to calculate the age of each employee as of today.Write a SELECT query that uses the
AGE function to calculate the time difference between the hire date and birth date for each employee.💡 Why This Matters
🌍 Real World
HR departments often need to calculate employee ages and tenure to manage benefits and compliance.
💼 Career
Knowing how to use date functions like AGE in SQL is essential for data analysts and database administrators working with time-based data.
Progress0 / 4 steps