OriginChain docs
examples · sql · 8 / 13 · works today

8. ORDER BY + LIMIT / OFFSET

← SQL examples
works today

ORDER BY (ascending or descending), LIMIT, and OFFSET all execute server-side through the SQL translator - the engine's Sort operator is wired through. Sorting and pagination happen in the engine, not your app.

top 3 by amount
POST /v1/tenants/:t/sql
# ORDER BY + LIMIT + OFFSET all execute server-side.
curl -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/sql" \
  -H "Authorization: Bearer $OC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "sql": "SELECT id, amount_cents FROM shop.orders WHERE amount_cents > 0 ORDER BY amount_cents DESC LIMIT 3"
  }'
pagination - LIMIT + OFFSET

Stable pagination over a sorted result: page N is ORDER BY ... LIMIT n OFFSET (N-1)*n.

POST /v1/tenants/:t/sql
# Page 2 (rows 11-20): ORDER BY + LIMIT + OFFSET.
curl -X POST "https://$OC_HOST/v1/tenants/$OC_TENANT/sql" \
  -H "Authorization: Bearer $OC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "sql": "SELECT id, amount_cents FROM shop.orders ORDER BY amount_cents DESC LIMIT 10 OFFSET 10"
  }'