Skip to content

SQL Reference

TensorDB provides a full SQL query surface over its append-only ledger storage. SQL is the primary interface for creating tables, inserting data, and querying.

Quick Demo

TensorDB SQL
SELECT name, email, created_at
FROM users
WHERE status = 'active'
ORDER BY created_at DESC
LIMIT 5;
Results 5 rows
nameemailcreated_at
Alice Chenalice@example.com2025-03-15 09:22:01
Bob Smithbob@example.com2025-03-14 14:05:33
Carol Wucarol@example.com2025-03-14 08:47:19
Dave Parkdave@example.com2025-03-13 22:11:45
Eve Joneseve@example.com2025-03-12 16:33:08
INSERT INTO transactions (id, account, amount, currency)
VALUES ('txn_001', 'acc_alice', 250.00, 'USD');

-- Every write becomes an immutable fact with:
--   commit_ts  = auto-assigned system timestamp
--   valid_from = now (or specified)
--   valid_to   = infinity
Results 1 row
result
1 row inserted (commit_ts: 1042)
-- Read the database as it was at commit 500
SELECT account, balance
FROM accounts
AS OF 500
WHERE account = 'acc_alice';
Results 1 row
accountbalance
acc_alice1,500.00
-- What was the policy rate valid on 2024-06-15?
SELECT policy_id, rate, valid_from, valid_to
FROM insurance_policies
VALID AT 1718409600
WHERE policy_id = 'POL-2024-001';
Results 1 row
policy_idratevalid_fromvalid_to
POL-2024-0014.2517040672001735689600
SELECT u.name, COUNT(o.id) as order_count,
       SUM(o.total) as total_spent
FROM users u
JOIN orders o ON u.id = o.user_id
GROUP BY u.name
HAVING total_spent > 100
ORDER BY total_spent DESC;
Results 3 rows
nameorder_counttotal_spent
Alice Chen122,450.00
Bob Smith81,823.50
Carol Wu5967.25
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE user_id = 'u_alice'
  AND status = 'shipped';
Results 7 rows
plan
Filter { predicate: status = 'shipped' }
└─ PointLookup { key: 'u_alice', cost: 1.00 }
execution_time_us: 42
rows_returned: 3
bloom_filter_hits: 1
cache_hits: 2

SQL Capabilities

CategoryFeatures
DDLCREATE TABLE, DROP TABLE, ALTER TABLE (ADD/DROP/RENAME COLUMN)
DMLINSERT, UPDATE (creates new version), DELETE (soft delete)
QueriesSELECT, WHERE, ORDER BY, LIMIT/OFFSET, GROUP BY, HAVING
JoinsINNER JOIN, LEFT JOIN, CROSS JOIN
SubqueriesScalar subqueries, IN subqueries, EXISTS
CTEsWITH … AS (common table expressions)
AggregatesCOUNT, SUM, AVG, MIN, MAX, GROUP_CONCAT
Window FunctionsROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, SUM/AVG OVER
TemporalAS OF (time travel), VALID AT (bitemporal)
ExplainEXPLAIN, EXPLAIN ANALYZE
PreparedParameterized queries with $1, $2, …
DiagnosticsSHOW STATS, SHOW SLOW QUERIES, SHOW ACTIVE QUERIES, SHOW STORAGE, SHOW COMPACTION STATUS
Functions60+ string, numeric, datetime, and utility functions

Sections