Brightlane's data pipeline validates reporting windows by confirming the number of days in a calendar year.
Write a query to return the number of days between '2024-01-01' and '2024-12-31'.
Output:
- A single row with one column,
days_in_year, containing the day count as a number.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
'2024-12-31'::date - '2024-01-01'::date AS days_in_year The shape
DATE - DATE returns an integer day count, and subtracting January 1 from December 31 of the same year returns 365, which matches the day count of a non-leap year. The result is a plain number, suitable for the pipeline's reporting-window check.
Clause by clause
SELECT '2024-12-31'::date - '2024-01-01'::date AS days_in_yearcasts both literals toDATE, subtracts the start of the year from the end, and returns the elapsed days. PostgreSQL counts from one date to the other, exclusive of the start and inclusive of the end. The aliasAS days_in_yearnames the result column.
The trap
365 is the number of days elapsed between January 1 and December 31, not the number of distinct calendar dates in the year. The year 2024 contains 366 distinct dates because it is a leap year, but the subtraction returns 365 because that is how many full days separate the two endpoints. A reader expecting "days in the year" to mean "count of dates that fall in the year" would read this result as off by one. The two questions are different. DATE - DATE answers the elapsed-days question. Counting distinct dates in a range needs a different construction.
You practiced date - date over a full calendar year — the result reflects elapsed days from start to end, not a count of distinct dates spanned.