← Back to SQL, page 11

sql-0209 · Easy fresher

Why might storing an age column instead of a birth date be a modeling mistake?

Age changes every year, so a stored age becomes wrong without any update, while a birth date stays true. Store the stable fact and compute the derived value at query time, or in a view. The same applies to 'days since signup' or 'is overdue' flags that depend on the current date.

Full answer

A stored age is correct only on the day it was written. A year later, every row is off by one unless a job rewrites them, and that job is one more thing to fail.

Store birth_date and compute age when needed, as shown in the date questions in this bank. A view can expose it as if it were a column:

sql
CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT, birth_date TEXT);
INSERT INTO people VALUES (1, 'Asha', '1996-12-01'), (2, 'Ben', '2001-06-15');
CREATE VIEW people_with_age AS
SELECT id, name, birth_date,
       CAST(strftime('%Y', '2026-12-01') AS INTEGER) - CAST(strftime('%Y', birth_date) AS INTEGER)
       - (strftime('%m-%d', '2026-12-01') < strftime('%m-%d', birth_date)) AS age
FROM people;
SELECT name, age FROM people_with_age ORDER BY id;
nameage
Asha30
Ben25

The view uses a fixed reference date here so the output is stable; in practice it would use date('now').

Values that depend on "now" cannot be generated columns either, because generated columns must be deterministic. Values derived only from the row itself, such as qty * unit_price, are fine to generate or store.

sql
CREATE TABLE people (id INTEGER PRIMARY KEY, name TEXT, birth_date TEXT);
INSERT INTO people VALUES (1, 'Asha', '1996-12-01'), (2, 'Ben', '2001-06-15');
CREATE VIEW people_with_age AS
SELECT id, name, birth_date,
       CAST(strftime('%Y', '2026-12-01') AS INTEGER) - CAST(strftime('%Y', birth_date) AS INTEGER)
       - (strftime('%m-%d', '2026-12-01') < strftime('%m-%d', birth_date)) AS age
FROM people;
SELECT name, age FROM people_with_age ORDER BY id;

Checked on SQLite 3.46.1, run twice with identical output.

Key points

  • Store stable facts, derive time-dependent values
  • A stored age silently becomes wrong
  • Views can expose computed values like columns
  • Generated columns cannot depend on the current date

Common mistakes

  • Storing age, tenure or overdue flags that depend on today
  • Running nightly jobs to refresh values that could be computed

Follow-ups

  • How would you index a query for people turning 18 this month?
  • Which derived values are safe to store?

Tips

  • Ask whether the value depends on the current date