Brightlane's contract management system reports the number of days a contract has been active.
Write a query to return the number of days between the start date '2024-01-01' and the end date '2024-03-15'.
Output:
- A single row with one column,
days_active, 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-03-15'::date - '2024-01-01'::date AS days_active The shape
Subtracting one DATE from another returns the integer number of days between them. The result is a plain number, not a duration type, which is exactly the day count the contract management report needs.
Clause by clause
SELECT '2024-03-15'::date - '2024-01-01'::date AS days_activecasts both literals toDATEand subtracts the earlier date from the later one. PostgreSQL counts the elapsed days and returns 74. The aliasAS days_activenames the column for the report.
Why this returns an integer and not an INTERVAL
DATE - DATE is a special case in PostgreSQL's date arithmetic. It produces a plain integer rather than an INTERVAL, because the result is unambiguous: a day count between two calendar dates has no hours, no months, no fractional component. Subtracting two TIMESTAMP values returns an INTERVAL instead, because timestamps can differ by hours and seconds as well as by days, and an INTERVAL is the type that can carry all of those components. The choice of return type follows the precision of the input type.
You practiced date - date returning an integer day count — PostgreSQL produces a plain number for date subtraction, not a duration type.