OriginChain docs
examples · sql · 2 / 13

2. WHERE = (equality filter)

← SQL examples
what this does

Return only the rows whose region column exactly equals the string 'IN'. Because we declared an index on region in the schema, the planner promotes this to an IndexScan - the matching row is found by direct lookup rather than by scanning every row.

when to use it
  • "Find the row(s) where this column equals this value" - the most common filter shape.
  • Lookups by user ID, status, region, category - any single equality predicate.
  • The column you're filtering on should be either the primary key or covered by a secondary index. Without an index, equality still works but becomes a full scan.
the request

Same schema and seed data as Example 1 - if you haven't loaded the customers yet, set those up first.

POST /v1/tenants/:t/sql
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, email, region FROM shop.customers WHERE region = '\''IN'\'' LIMIT 10"
  }'
what you get back
{
  "kind": "select",
  "rows": [
    { "id": "c_1", "email": "alice@example.com", "region": "IN" }
  ]
}
how it works
  • The translator builds a Filter(region = 'IN') → Scan(shop.customers) plan.
  • The optimiser checks the schema for indexes covering the filtered column. by_region covers region, so the plan is rewritten to IndexScan(by_region, region = 'IN').
  • The IndexScan goes directly to the matching keys instead of iterating the whole row store. Lookup is sub-linear instead of linear in row count.

Run EXPLAIN SELECT ... on the same query to see whether your indexes are being used.

common mistakes
  • Quoting strings as double-quoted. SQL strings use single quotes - WHERE region = 'IN'. Double-quoted identifiers refer to columns, not strings.
  • Case sensitivity. String equality is case-sensitive: 'IN' doesn't match 'in'. Lowercase at write time if you want case-insensitive lookups.
  • Combining with OR. The engine only supports AND in WHERE today. For OR, rewrite as IN (...) (next example) or run two queries.