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 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 duckdb
print(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: it's kept as you wrote it, so you can compare. Only you and your Maestro see your predictions.
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. If the exact day matters in your case, decide the rule and write it down.
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.
Calculate each customer's age on June 30, 2026, in whole years, sorted by clienteid.
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 |
Every run starts from fresh copies of the course's tables, so try anything. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your query.
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.
Calculate each customer's age on June 30, 2026 in whole years and print the name and age of the first five.
Calculate each customer's age on June 30, 2026 in whole years and print the name and age of the first five.
Run your code to see what it prints and the value of its last expression. Every run starts fresh. Ctrl+Enter runs it. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your code.
Run your code to see its output here.
Data: clientes-frecuentes, 24 rows
Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.
| Row | Remove | |||||
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | ||||||
| 3 | ||||||
| 4 | ||||||
| 5 | ||||||
| 6 | ||||||
| 7 | ||||||
| 8 | ||||||
| 9 | ||||||
| 10 | ||||||
| 11 | ||||||
| 12 | ||||||
| 13 | ||||||
| 14 | ||||||
| 15 | ||||||
| 16 | ||||||
| 17 | ||||||
| 18 | ||||||
| 19 | ||||||
| 20 |