Prompt
How do I use SQL with a time-series database?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Using SQL with a time-series database is usually very similar to using SQL in a relational database, but with a few time-series-specific patterns and functions.
Basic idea
A time-series database stores data that changes over time, such as:
- sensor readings
- application metrics
- financial prices
- logs/events
You typically model each row as:
- timestamp
- entity/device/series id
- measurement value(s)
Example table:
CREATE TABLE metrics (
ts TIMESTAMP NOT NULL,
device_id TEXT NOT NULL,
temperature DOUBLE PRECISION,
humidity DOUBLE PRECISION
);
Common SQL queries
1. Insert time-series data
INSERT INTO metrics (ts, device_id, temperature, humidity)
VALUES ('2026-10-25 12:00:00', 'device-1', 22.5, 40.2);
2. Query recent data
SELECT *
FROM metrics
WHERE device_id = 'device-1'
AND ts >= NOW() - INTERVAL '1 hour'
ORDER BY ts;
3. Aggregate over time buckets
This is one of the most common time-series patterns.
SELECT
DATE_TRUNC('minute', ts) AS minute,
AVG(temperature) AS avg_temp
FROM metrics
WHERE device_id = 'device-1'
AND ts >= NOW() - INTERVAL '1 day'
GROUP BY minute
ORDER BY minute;
Some databases provide special bucket functions, such as:
DATE_TRUNC(...)time_bucket(...)in TimescaleDB- custom window/bucket functions in other TSDBs
4. Use window functions
To calculate moving averages or deltas:
SELECT
ts,
temperature,
AVG(temperature) OVER (
PARTITION BY device_id
ORDER BY ts
ROWS BETWEEN 4 PRECEDING AND CURRENT ROW
) AS moving_avg
FROM metrics
WHERE device_id = 'device-1';
5. Find latest value per device
SELECT DISTINCT ON (device_id) device_id, ts, temperature
FROM metrics
ORDER BY device_id, ts DESC;
Or using a window function:
SELECT device_id, ts, temperature
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY ts DESC) AS rn
FROM metrics
) t
WHERE rn = 1;
Time-series-specific features to look for
Many time-series databases add SQL extensions for:
- time bucketing (
time_bucket,DATE_TRUNC) - downsampling / rollups
- gap filling
- retention policies
- continuous aggregates/materialized views
- time partitioning
Best practices
- Always index or partition by timestamp
- Often also index by the most common filter key, like
device_id - Use append-only writes when possible
- Avoid updating historical rows unless necessary
- Pre-aggregate if queries are frequent over large ranges
- Keep timestamps in UTC
Example in TimescaleDB
If you're using PostgreSQL + TimescaleDB:
SELECT time_bucket('5 minutes', ts) AS bucket,
AVG(temperature)
FROM metrics
WHERE device_id = 'device-1'
GROUP BY bucket
ORDER BY bucket;
If you tell me the database
The exact SQL depends on the engine:
- TimescaleDB
- InfluxDB SQL/InfluxQL
- ClickHouse
- QuestDB
- Apache Druid
- PostgreSQL with time-series extensions
If you want, I can show:
- a schema design for a time-series table,
- example queries for a specific database, or
- how to write efficient time-series SQL.