To group timestamps by a fixed interval, you would use which function?

Study for the Database Systems Test. Prepare with flashcards and multiple choice questions, each with hints and explanations. Get ready for success!

Multiple Choice

To group timestamps by a fixed interval, you would use which function?

Explanation:
Grouping timestamps by a fixed interval is done by creating buckets that align to the start of each interval. The function date_trunc does exactly this: it takes a timestamp and truncates it to the start of the specified interval (like hour, day, minute, etc.). This produces consistent bucket boundaries that you can use in GROUP BY to aggregate data. For example, to count events per hour, you would bucket the timestamps with date_trunc('hour', ts) and group by that value: SELECT date_trunc('hour', ts) AS hour_bucket, COUNT(*) FROM events GROUP BY hour_bucket ORDER BY hour_bucket; This works because date_trunc returns a new timestamp aligned to the start of the chosen interval, making all times within the same interval map to the same bucket. The other options don’t serve this bucketing purpose: NOW returns the current timestamp, not a fixed-interval bucket; make_date/time construct date or time values from components and aren’t used for grouping a timestamp column; age computes the interval between two timestamps and isn’t used to create interval buckets.

Grouping timestamps by a fixed interval is done by creating buckets that align to the start of each interval. The function date_trunc does exactly this: it takes a timestamp and truncates it to the start of the specified interval (like hour, day, minute, etc.). This produces consistent bucket boundaries that you can use in GROUP BY to aggregate data.

For example, to count events per hour, you would bucket the timestamps with date_trunc('hour', ts) and group by that value:

SELECT date_trunc('hour', ts) AS hour_bucket, COUNT(*)

FROM events

GROUP BY hour_bucket

ORDER BY hour_bucket;

This works because date_trunc returns a new timestamp aligned to the start of the chosen interval, making all times within the same interval map to the same bucket.

The other options don’t serve this bucketing purpose: NOW returns the current timestamp, not a fixed-interval bucket; make_date/time construct date or time values from components and aren’t used for grouping a timestamp column; age computes the interval between two timestamps and isn’t used to create interval buckets.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy