← All lessons/Querying with SaQL
11
Querying with SaQL

SaQL Basics

The SaQL query language: all query types, the request shape, and basic filter syntax.

Prerequisite: Lesson 6 complete

What you'll learn

  • POST /v1/agents/:id/query — the universal query endpoint
  • All 12 query types: latest, first, scan, count, chain_head, as_of, time_range, distinct, aggregate, group_by, explain, join
  • Filter shape: {field, op, value}
  • Combining filter with limit, order, after_hlc, select
Challenge

Write 10 records to products agent with category and price. Count how many have category = 'electronics'.

Interactive Walkthrough

What you'll learn

The SaQL query language: query types, the request shape, and the basic filter syntax.

SaQL request shape

All SaQL queries are POST /v1/agents/:id/query with a JSON body:

{
  "type": "latest | first | scan | count | chain_head | as_of | time_range | ...",
  "limit": 20,
  "filter": { ... },
  "order": "asc | desc",
  "after_hlc": 0,
  "select": ["field1", "field2"]
}

All query types

TypeReturns
latestNewest N records
firstOldest N records
scanFull strand, windowed
countInteger only
chain_headSingle tip record
as_ofRecords up to a timestamp
time_rangeRecords in a time window
distinctUnique values for a field
aggregatesum/avg/min/max/count across a field
group_byCount/aggregate per group
explainQuery plan without executing
joinInner join across two agents

Simple filter

curl -s -X POST http://localhost:7475/v1/agents/users/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "type": "scan",
    "limit": 50,
    "filter": {"field": "plan", "op": "eq", "value": "pro"}
  }' | python3 -m json.tool

count with filter

curl -s -X POST http://localhost:7475/v1/agents/users/query \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \
  -d '{
    "type": "count",
    "filter": {"field": "plan", "op": "eq", "value": "enterprise"}
  }' | python3 -m json.tool

Challenge

Write 10 records to a products agent with varying category and price fields. Count how many have category = "electronics".

---

← Previous
Lesson 10: Schema Validation
Next →
Lesson 12: All 15 WhereOp Operators