Date arithmetic with intervals in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to do date calculations changes as we work with more data.
Specifically, how adding or subtracting intervals from dates behaves when done many times.
Analyze the time complexity of the following code snippet.
WITH dates AS (
SELECT generate_series(1, 1000) AS day_num
)
SELECT day_num,
(CURRENT_DATE + (day_num || ' days')::interval) AS new_date
FROM dates;
This code generates 1000 numbers and adds that many days as intervals to the current date.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding an interval to a date for each number generated.
- How many times: Once for each row generated, here 1000 times.
Each additional row means one more date plus interval calculation.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 date + interval additions |
| 100 | 100 date + interval additions |
| 1000 | 1000 date + interval additions |
Pattern observation: The work grows directly with the number of rows; double the rows, double the calculations.
Time Complexity: O(n)
This means the time to complete grows in a straight line as the number of date calculations increases.
[X] Wrong: "Adding intervals to dates happens instantly no matter how many rows there are."
[OK] Correct: Each addition is a separate calculation, so more rows mean more work and more time.
Understanding how operations scale with data size helps you explain and predict query performance clearly and confidently.
"What if we changed the interval from days to months? How would the time complexity change?"
Practice
SELECT CURRENT_DATE + INTERVAL '5 days';Solution
Step 1: Understand the CURRENT_DATE keyword
CURRENT_DATE returns today's date without time.Step 2: Add interval of 5 days
Adding INTERVAL '5 days' increases the date by 5 days.Final Answer:
Adds 5 days to the current date -> Option DQuick Check:
Adding interval '5 days' = date + 5 days [OK]
- Confusing addition with subtraction
- Thinking CURRENT_DATE includes time
- Using wrong interval syntax
order_date?Solution
Step 1: Identify correct interval syntax
INTERVAL must be followed by a quoted string with number and unit, e.g., '3 months'.Step 2: Check subtraction from date column
Subtracting INTERVAL '3 months' from order_date is valid syntax.Final Answer:
order_date - INTERVAL '3 months' -> Option BQuick Check:
Subtract INTERVAL '3 months' with quotes [OK]
- Omitting quotes around interval string
- Using singular 'month' instead of plural 'months'
- Trying to subtract string without INTERVAL keyword
SELECT (DATE '2024-06-15' + INTERVAL '1 month' - INTERVAL '10 days')::date AS new_date;
Solution
Step 1: Add 1 month to 2024-06-15
Adding 1 month results in 2024-07-15.Step 2: Subtract 10 days from 2024-07-15
Subtracting 10 days gives 2024-07-05.Final Answer:
2024-07-05 -> Option CQuick Check:
2024-06-15 + 1 month - 10 days = 2024-07-05 [OK]
- Subtracting before adding
- Confusing day and month units
- Not casting to date and including time
SELECT CURRENT_DATE + INTERVAL 5 days;
Solution
Step 1: Check INTERVAL syntax
INTERVAL requires the value to be a quoted string, e.g., '5 days'.Step 2: Identify missing quotes error
The query lacks quotes around 5 days, causing syntax error.Final Answer:
Missing quotes around interval value -> Option AQuick Check:
INTERVAL '5 days' needs quotes [OK]
- Writing INTERVAL 5 days without quotes
- Assuming CURRENT_DATE can't be used with intervals
- Confusing INTERVAL keyword spelling
orders where the order_date is within the last 3 months from today. Which query correctly uses date arithmetic with intervals?Solution
Step 1: Understand the requirement
We want orders from the last 3 months, so order_date should be after (or equal to) 3 months ago.Step 2: Use CURRENT_DATE minus 3 months
Subtracting INTERVAL '3 months' from CURRENT_DATE gives the date 3 months ago.Step 3: Write condition for order_date
order_date >= CURRENT_DATE - INTERVAL '3 months' selects orders within last 3 months.Final Answer:
SELECT * FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '3 months'; -> Option AQuick Check:
Filter dates after CURRENT_DATE - 3 months [OK]
- Using + INTERVAL instead of - INTERVAL
- Using < instead of >= for filtering recent dates
- Confusing direction of date comparison
Ready to prove it?
Pass the test and this pattern moves unknown
