SQL Query Examples
Ready-to-use SQL examples for common time series operations. All examples use POST requests for clarity,
but GET requests with ?sql= parameter also work for simple queries.
💡 Tip
Use short table names (e.g., cpu) instead of fully qualified names. The system auto-prefixes with catalog and schema.
Basic Queries
Select All Data
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu LIMIT 10'
Select Specific Columns
curl 'http://localhost:7000/query/mydb' -d 'SELECT host, usage, time FROM cpu'
Order By
# Highest usage first
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu ORDER BY usage DESC LIMIT 10'
# Latest records first
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu ORDER BY time DESC LIMIT 10'
Distinct Values
# Unique hosts
curl 'http://localhost:7000/query/mydb' -d 'SELECT DISTINCT host FROM cpu'
# Count unique hosts
curl 'http://localhost:7000/query/mydb' -d 'SELECT COUNT(DISTINCT host) as unique_hosts FROM cpu'
Aggregations
Basic Aggregates
# Count
curl 'http://localhost:7000/query/mydb' -d 'SELECT COUNT(*) as total FROM cpu'
# Average
curl 'http://localhost:7000/query/mydb' -d 'SELECT AVG(usage) as avg_usage FROM cpu'
# Min / Max
curl 'http://localhost:7000/query/mydb' -d 'SELECT MIN(usage) as min, MAX(usage) as max FROM cpu'
# Sum
curl 'http://localhost:7000/query/mydb' -d 'SELECT SUM(usage) as total_usage FROM cpu'
GROUP BY
# Average by host
curl 'http://localhost:7000/query/mydb' -d 'SELECT host, AVG(usage) as avg_cpu FROM cpu GROUP BY host'
# Multiple aggregates by host
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
COUNT(*) as count,
AVG(usage) as avg,
MIN(usage) as min,
MAX(usage) as max
FROM cpu
GROUP BY host
"
HAVING
# Hosts with average usage > 50%
curl 'http://localhost:7000/query/mydb' -d "
SELECT host, AVG(usage) as avg_cpu
FROM cpu
GROUP BY host
HAVING AVG(usage) > 50
"
Percentiles
curl 'http://localhost:7000/query/mydb' -d "
SELECT
approx_percentile_cont(usage, 0.50) as p50,
approx_percentile_cont(usage, 0.95) as p95,
approx_percentile_cont(usage, 0.99) as p99
FROM cpu
"
Filtering
WHERE Clause
# By numeric value
curl 'http://localhost:7000/query/mydb' -d 'SELECT * FROM cpu WHERE usage > 80'
# By string value
curl 'http://localhost:7000/query/mydb' -d "SELECT * FROM cpu WHERE host = 'server01'"
# Multiple conditions (AND)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > 50 AND region = 'us-west'
"
# Multiple conditions (OR)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE region = 'us-west' OR region = 'us-east'
"
Time Range Queries
# After time
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE time > 1703251234567000000
ORDER BY time DESC
"
# Between times
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE time BETWEEN 1703251234567000000 AND 1703251300000000000
"
IN and NOT IN
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE host IN ('server01', 'server02', 'server03')
"
LIKE Pattern Matching
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE host LIKE 'server%'
"
JOIN Queries
INNER JOIN
curl 'http://localhost:7000/query/mydb' -d "
SELECT c.host, c.usage as cpu_usage, m.used as memory_used
FROM cpu c
JOIN memory m ON c.host = m.host
"
LEFT JOIN
curl 'http://localhost:7000/query/mydb' -d "
SELECT c.host, c.usage, m.used
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
"
Multiple JOINs
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.usage as cpu_usage,
m.used as memory_used,
d.free as disk_free
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
LEFT JOIN disk d ON c.host = d.host
"
Window Functions
Moving Average
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
host,
usage,
AVG(usage) OVER (
PARTITION BY host
ORDER BY time
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) as moving_avg
FROM cpu
"
Ranking
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
usage,
RANK() OVER (ORDER BY usage DESC) as rank,
DENSE_RANK() OVER (ORDER BY usage DESC) as dense_rank
FROM cpu
"
Running Total
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
usage,
SUM(usage) OVER (ORDER BY time) as running_total
FROM cpu
"
Lag / Lead
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
usage,
LAG(usage, 1) OVER (ORDER BY time) as prev_usage,
LEAD(usage, 1) OVER (ORDER BY time) as next_usage
FROM cpu
"
Advanced Queries
Subqueries
# Find above-average usage
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > (SELECT AVG(usage) FROM cpu)
"
# Find anomalies (> 2x average)
curl 'http://localhost:7000/query/mydb' -d "
SELECT * FROM cpu
WHERE usage > 2 * (SELECT AVG(usage) FROM cpu)
"
Common Table Expressions (CTE)
curl 'http://localhost:7000/query/mydb' -d "
WITH host_stats AS (
SELECT
host,
AVG(usage) as avg_usage,
MAX(usage) as max_usage
FROM cpu
GROUP BY host
)
SELECT * FROM host_stats
WHERE avg_usage > 50
ORDER BY avg_usage DESC
"
CASE Statements
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
usage,
CASE
WHEN usage < 30 THEN 'low'
WHEN usage < 70 THEN 'medium'
ELSE 'high'
END as usage_level
FROM cpu
"
UNION
curl 'http://localhost:7000/query/mydb' -d "
SELECT host, 'cpu' as metric, usage as value FROM cpu
UNION ALL
SELECT host, 'memory' as metric, used as value FROM memory
"
Time Bucketing
# Group by minute
curl 'http://localhost:7000/query/mydb' -d "
SELECT
(time / 60000000000) as minute_bucket,
AVG(usage) as avg_usage
FROM cpu
GROUP BY (time / 60000000000)
ORDER BY minute_bucket
"
Practical Examples
Top 10 Hosts by CPU Usage
curl 'http://localhost:7000/query/mydb' -d "
SELECT
host,
AVG(usage) as avg_cpu,
MAX(usage) as max_cpu,
COUNT(*) as samples
FROM cpu
GROUP BY host
ORDER BY avg_cpu DESC
LIMIT 10
"
Resource Utilization Summary
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.region,
ROUND(AVG(c.usage), 2) as avg_cpu,
ROUND(AVG(m.used / m.total * 100), 2) as avg_memory_pct
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
GROUP BY c.host, c.region
ORDER BY avg_cpu DESC
"
High CPU and Memory Alerts
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.usage as cpu_usage,
m.used as memory_used
FROM cpu c
JOIN memory m ON c.host = m.host
WHERE c.usage > 80 AND m.used > 80
ORDER BY c.usage DESC
"
Compare Regions
curl 'http://localhost:7000/query/mydb' -d "
SELECT
region,
COUNT(DISTINCT host) as hosts,
AVG(usage) as avg_usage,
MIN(usage) as min_usage,
MAX(usage) as max_usage
FROM cpu
GROUP BY region
"
Latest Reading per Host
curl 'http://localhost:7000/query/mydb' -d "
WITH ranked AS (
SELECT
host,
usage,
time,
ROW_NUMBER() OVER (PARTITION BY host ORDER BY time DESC) as rn
FROM cpu
)
SELECT host, usage, time
FROM ranked
WHERE rn = 1
"
Usage Change Detection
curl 'http://localhost:7000/query/mydb' -d "
SELECT
time,
host,
usage,
LAG(usage) OVER (PARTITION BY host ORDER BY time) as prev_usage,
usage - LAG(usage) OVER (PARTITION BY host ORDER BY time) as change
FROM cpu
WHERE host = 'server01'
ORDER BY time DESC
LIMIT 20
"
Complete Workflow Example
# 1. Write sample data
curl -X POST http://localhost:7000/write/mydb -d '
cpu,host=server01,region=us-west usage=75.5,temp=45
cpu,host=server02,region=us-west usage=45.2,temp=38
cpu,host=server03,region=us-east usage=89.1,temp=52
memory,host=server01 used=16.5,total=32.0
memory,host=server02 used=24.8,total=64.0
memory,host=server03 used=12.2,total=32.0
'
# 2. Flush to disk
curl -X POST http://localhost:7000/flush
# 3. Query data
curl 'http://localhost:7000/query/mydb' -d "
SELECT
c.host,
c.region,
c.usage as cpu_usage,
m.used as memory_used,
CASE
WHEN c.usage > 80 THEN 'critical'
WHEN c.usage > 60 THEN 'warning'
ELSE 'normal'
END as status
FROM cpu c
LEFT JOIN memory m ON c.host = m.host
ORDER BY c.usage DESC
"