DATE_TRUNC for rounding dates in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to round dates using DATE_TRUNC changes as we work with more data.
How does the work grow when we apply DATE_TRUNC to many rows?
Analyze the time complexity of the following code snippet.
SELECT DATE_TRUNC('month', order_date) AS month_start,
COUNT(*) AS orders_count
FROM orders
GROUP BY month_start
ORDER BY month_start;
This query rounds each order date down to the first day of its month, then counts orders per month.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying DATE_TRUNC to each row's date in the orders table.
- How many times: Once for every row in the orders table.
As the number of rows grows, the database must round more dates, so work grows steadily.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 date rounds |
| 100 | 100 date rounds |
| 1000 | 1000 date rounds |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles the work.
Time Complexity: O(n)
This means the time to round dates grows in a straight line with the number of rows processed.
[X] Wrong: "DATE_TRUNC runs once and applies to all rows instantly."
[OK] Correct: Each row's date must be processed separately, so the work adds up with more rows.
Understanding how functions like DATE_TRUNC scale helps you explain query performance clearly and confidently.
"What if we added an index on the order_date column? How would that affect the time complexity of this query?"
Practice
DATE_TRUNC('month', timestamp) do?Solution
Step 1: Understand DATE_TRUNC behavior
DATE_TRUNC cuts off smaller parts of a timestamp to the specified precision, rounding down.Step 2: Apply 'month' precision
Using 'month' sets the date to the first day of that month at midnight (00:00:00).Final Answer:
It rounds the timestamp down to the first day of the month at 00:00:00. -> Option BQuick Check:
DATE_TRUNC('month', timestamp) = first day of month [OK]
- Thinking it rounds up instead of down
- Assuming it returns a string
- Believing it keeps the original timestamp unchanged
Solution
Step 1: Check function syntax
DATE_TRUNC requires the first argument as a string specifying the precision, e.g., 'hour'.Step 2: Validate timestamp format
The second argument must be a timestamp or string representing a timestamp enclosed in quotes.Final Answer:
DATE_TRUNC('hour', '2024-06-15 13:45:30') -> Option DQuick Check:
Correct syntax uses quotes around precision and timestamp [OK]
- Not quoting the precision string
- Passing timestamp without quotes
- Using incomplete timestamp formats
SELECT DATE_TRUNC('day', TIMESTAMP '2024-06-15 18:23:45');What is the output?
Solution
Step 1: Understand 'day' precision in DATE_TRUNC
Truncating to 'day' sets the time to midnight (00:00:00) of the same date.Step 2: Apply to given timestamp
For '2024-06-15 18:23:45', truncation results in '2024-06-15 00:00:00'.Final Answer:
2024-06-15 00:00:00 -> Option CQuick Check:
DATE_TRUNC('day', timestamp) = date at midnight [OK]
- Expecting original time to remain
- Confusing day truncation with month truncation
- Thinking it rounds to nearest day
SELECT DATE_TRUNC('min', '2024-06-15 12:34:56');Solution
Step 1: Check precision argument
DATE_TRUNC requires the first argument to be a specific precision string (e.g., 'minute', 'hour'). 'min' is invalid.Step 2: Identify the error
This causes an error like 'invalid value for parameter "field": "min"'.Final Answer:
The precision 'min' is invalid in DATE_TRUNC. -> Option AQuick Check:
Valid precisions include 'minute' [OK]
- Assuming string is auto-converted
- Using invalid precision values
- Adding extra arguments to DATE_TRUNC
DATE_TRUNC. Which query correctly truncates the timestamp to the start of the week?Solution
Step 1: Understand DATE_TRUNC('week') behavior
In PostgreSQL, DATE_TRUNC('week', timestamp) returns the start of the week on Sunday.Step 2: Adjust to Monday start
Adding 1 day before truncation shifts the week start to Monday, then subtracting 1 day corrects the date.Final Answer:
SELECT DATE_TRUNC('week', sale_date + INTERVAL '1 day') - INTERVAL '1 day' FROM sales; -> Option AQuick Check:
Shift date to align week start Monday [OK]
- Assuming 'week' starts on Monday by default
- Using invalid precision 'dow'
- Truncating to month instead of week
Ready to prove it?
Pass the test and this pattern moves unknown
