Exact Age
Return customer_id and age (in whole years) on 15 June 2026 for customers 1 to 10, sorted by customer_id. A person who has not yet had their birthday that year is one year younger.
Do not use today's date: the answer must stay the same every day.
Hint 1
The year difference is a first guess. It is one too high if the birthday (month and day) comes after 15 June.
Hint 2
STRFTIME('%m-%d', birth_date) gives the month and day, and ISO text sorts correctly, so you can compare it with '06-15'.
Reference solution
SELECT customer_id,
2026 - CAST(STRFTIME('%Y', birth_date) AS INTEGER)
- (STRFTIME('%m-%d', birth_date) > '06-15') AS age
FROM customers
WHERE customer_id BETWEEN 1 AND 10
ORDER BY customer_id;
The exact age is year difference - 1 when the birthday has not happened yet this year, otherwise just the year difference. In SQLite a comparison returns 1 (true) or 0 (false), so it can be subtracted directly. Comparing the text 'MM-DD' works because ISO format sorts in calendar order. Age calculations are a favourite interview question because the obvious answer is wrong for part of the year.
Another way. Dividing the days by 365.25 is a common approximation. It can be wrong by one year for people whose birthday is within a day or two of the date, so prefer the exact method in real reports.
SELECT customer_id,
CAST((JULIANDAY('2026-06-15') - JULIANDAY(birth_date)) / 365.25 AS INTEGER) AS age
FROM customers
WHERE customer_id BETWEEN 1 AND 10
ORDER BY customer_id;