OriginChainDB docs
examples · sql

SQL examples

← All examples

SQL runs over one endpoint - POST /v1/tenants/:t/sql - and comes back as JSON rows keyed by column name. The 13 examples below each carry the schema TOML they need, a pre-seed insert, side-by-side cURL / Python / TypeScript / Go, the response shape, and notes on common mistakes.

All 13 execute today. ORDER BY, HAVING, window functions (including explicit ROWS frames), UPDATE, DELETE and both plain and recursive CTEs all run server-side. Each shape has bounds — RANGE frames take peer bounds only, a recursive CTE is UNION ALL and one CTE per statement, and windows cannot yet share a SELECT with a JOIN or GROUP BY. The per-example pages state the limits that apply.

1
SELECT with projection
works today

Project specific columns from a single table. Read only the fields you need.

2
WHERE = on an indexed column
works today

Equality predicate. The planner promotes it to an IndexScan when an index covers the column.

3
WHERE col IN (...)
works today

Match against a small set of literal values. Folded into a disjunction over equalities.

4
WHERE col BETWEEN low AND high
works today

Range predicate on a numeric or string column. Inclusive on both bounds.

5
INNER JOIN - two tables
works today

Combine rows that have a match on both sides. Hash-join on the equality.

6
LEFT JOIN - keep unmatched left rows
works today

Every row from the left side, plus matches from the right (null if no match).

7
GROUP BY + COUNT / SUM / AVG / MIN / MAX
works today

Multiple aggregates in one pass. The hash aggregator buckets on the GROUP BY key.

8
ORDER BY + LIMIT / OFFSET
works today

ORDER BY (asc/desc), LIMIT and OFFSET all execute through the SQL translator today.

9
Window functions
works today

ROW_NUMBER, RANK, LAG and LEAD execute today, and so do explicit ROWS frames on the aggregates. RANGE takes peer bounds only.

10
Uncorrelated IN (SELECT ...)
works today

Uncorrelated and correlated subqueries both execute - IN (SELECT), EXISTS / NOT EXISTS, and scalar forms.

11
UPDATE via /sql
works today

UPDATE executes against the engine and returns rows_affected. (Earlier builds only translated.)

12
DELETE via /sql
works today

DELETE executes and returns rows_affected. Any supported predicate works, plus RETURNING.

13
WITH RECURSIVE
works today

Recursive CTEs walk a hierarchy to a fixed point: one CTE per statement, UNION ALL, depth and row capped.

14
Newer SQL recipes
new

CTEs, bind parameters, date_trunc time-bucketing, CASE and catalog introspection — the most recent additions, each shown against live output.