← All lessons/Querying with SaQL
17
Querying with SaQL

Distinct Queries

Find unique values for a field without loading all records.

Prerequisite: Lesson 11 complete

What you'll learn

  • type: distinct with field
  • Response: {distinct: {field, values[], count}}
  • Distinct with filter — scoped to matching records
  • Use case: find unique plans, countries, event types
  • Combining distinct + group_by for full frequency distribution
Challenge

Write 15 records with source: google, twitter, referral, direct. Distinct to get unique sources. Group_by to count per source.

Interactive Walkthrough

What you'll learn

Find unique values for a field without loading all records.

Distinct query shape

{
  "type":   "distinct",
  "field":  "plan",
  "limit":  100,
  "filter": { ... }
}

Find all unique plan values

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": "distinct", "field": "plan"}' \
  | python3 -m json.tool

Response: `json { "distinct": { "field": "plan", "values": ["enterprise", "free", "pro", "starter"], "count": 4 } } `

Find unique countries of paying users only

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": "distinct",
    "field": "country",
    "filter": {
      "NOT": {"field": "plan", "op": "eq", "value": "free"}
    }
  }' | python3 -m json.tool

Challenge

Write 15 records with a source field set to values like google, twitter, referral, direct. Use distinct to get unique sources. Then use group_by on source to count records per source.

---

← Previous
Lesson 16: Group By
Next →
Lesson 18: Single-Field Indexes