How do I calculate someone's age in SQL and pandas?
Calculate age in completed years with date_sub in DuckDB and with a birthday check in pandas, and test it on the cases where subtracting years gets it wrong.
Free course. Your Maestro, an AI, .
How old is Ana?
Marketing wants the customers broken down by age. Ana Ruiz was born on July 1, 1990. On June 30, 2026, our "today", how old is she?
Any person would say 35: she turns 36 tomorrow. A query won't always say the same. Predict what four ways of calculating it give you.
Four calculations of Ana Ruiz's age on June 30, 2026. Write what you think each one gives and why.
Four ways to calculate an age
import duckdbprint(duckdb.sql(""" SELECT 2026 - 1990 AS resta_de_anios, date_diff('year', DATE '1990-07-01', DATE '2026-06-30') AS con_date_diff, (DATE '2026-06-30' - DATE '1990-07-01') // 365 AS dias_entre_365, date_sub('year', DATE '1990-07-01', DATE '2026-06-30') AS con_date_sub""").fetchall())A guess is fine. What you write stays in this browser and waits in your course.
Age is a birthday question
Age is the number of full years since birth. Here's how you calculate it: subtract the years, and if this year's birthday hasn't come yet, take one off. That's exactly what date_sub('year', birth, reference) does in DuckDB: it counts whole years.
The shortcuts fail in the days before a birthday, and there are plenty of those days. Among the 24 customers, subtracting the years gives the wrong age for 12 of them, and dividing the days by 365 gets 2 wrong. The fact that the shortcut is usually right is what makes it dangerous: a quick check won't catch it.
Two cases to test any age formula against:
- The birthday itself. Carla Méndez was born on June 30, 1985: on June 30, 2026 she's already 41.
- Born on February 29. Beto Garza was born on February 29, 2000.
date_sub('year', DATE '2000-02-29', DATE '2026-02-28')gives 26: in a year that isn't a leap year, DuckDB treats February 28 as his birthday. This course chooses that same convention in both tools: the birthday is February 28 when the reference year is not a leap year, and February 29 when it is. The simple month/day comparison in pandas would wait until March 1, so we adjust it below. This is a convention for the exercise, not a universal legal rule.
Surprised that date_diff gave 36? Ask your Maestro what it's actually counting.
An age in pandas
Worked example · 4 steps
pandas has no date_sub. Calculate each customer's age on June 30, 2026 from the date columns.
Work out the next step yourself, then open it to compare.
Try it
The query calculates each customer's age with date_diff. Change it to count whole years. A good answer has 24 rows, with 35 for Ana Ruiz and 41 for Carla Méndez.
Your turn
Calculate each customer's age on June 30, 2026, in whole years, sorted by cliente_id.
Tables: clientes (cliente_id, nombre, fecha_nacimiento, fecha_alta, ultima_visita)
clientes · 24 rows
| Column | Type |
|---|---|
| cliente_id | BIGINT |
| nombre | VARCHAR |
| fecha_nacimiento Fecha de nacimiento. | DATE |
| fecha_alta Día en que se inscribió al programa de clientes frecuentes. | DATE |
| ultima_visita Día de su visita más reciente. | DATE |
First rows
| cliente_id | nombre | fecha_nacimiento | fecha_alta | ultima_visita |
|---|---|---|---|---|
| 1 | Ana Ruiz | 1990-07-01 | 2026-01-31 | 2026-02-01 |
| 2 | Beto Garza | 2000-02-29 | 2025-01-28 | 2025-06-15 |
| 3 | Carla Méndez | 1985-06-30 | 2024-02-06 | 2025-10-21 |
Tab indents; Esc then Tab leaves
Try it on your own
Now in pandas: build the edad (age) column as in the worked example, without copying it if you can, and print the name and age of the first five customers. A good answer starts with Ana Ruiz, 35. If some of your ages come out a year too high, share your code with your Maestro.
Your turn
Calculate each customer's age on June 30, 2026 in whole years and print the name and age of the first five. Use the course convention for February 29 births and also check it with reference date 2026-02-28.
Data: clientes-frecuentes, 24 rows
| Row | Remove | |||||
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | ||||||
| 3 | ||||||
| 4 | ||||||
| 5 | ||||||
| 6 | ||||||
| 7 | ||||||
| 8 | ||||||
| 9 | ||||||
| 10 | ||||||
| 11 | ||||||
| 12 | ||||||
| 13 | ||||||
| 14 | ||||||
| 15 | ||||||
| 16 | ||||||
| 17 | ||||||
| 18 | ||||||
| 19 | ||||||
| 20 |
Enter saves; Esc undoes; empty is a missing value
Tab indents; Esc then Tab leaves
Check yourself: Carla Méndez was born on June 30, 1985. How old is she on June 30, 2026?