← All lessons/Querying with SaQL
13
Querying with SaQL

Compound Filters (AND / OR / NOT)

Combine multiple filter conditions into complex logical expressions.

Prerequisite: Lesson 12 complete

What you'll learn

  • AND: all conditions must match
  • OR: at least one condition must match
  • NOT: inverts a condition
  • Nesting: AND inside OR, OR inside AND — any depth
  • Mixing leaf filters with compound filters
Challenge

Write 20 records with type, status, amount. Find: type='order' AND (status='paid' OR amount > 500).

Interactive Walkthrough

What you'll learn

Combine multiple filter conditions into complex logical expressions.

Filter shape

A filter is either a Leaf (one condition) or a Compound (AND/OR/NOT of sub-filters):

`json // Leaf {"field": "plan", "op": "eq", "value": "pro"}

// AND {"AND": [ {"field": "plan", "op": "eq", "value": "pro"}, {"field": "amount", "op": "gt", "value": 100} ]}

// OR {"OR": [ {"field": "plan", "op": "eq", "value": "free"}, {"field": "plan", "op": "eq", "value": "starter"} ]}

// NOT {"NOT": {"field": "status", "op": "eq", "value": "deleted"}} `

Nested example — pro users who spent more than $100 OR enterprise users

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": {
      "OR": [
        {
          "AND": [
            {"field": "plan",       "op": "eq", "value": "pro"},
            {"field": "lifetime_spend", "op": "gt", "value": 100}
          ]
        },
        {"field": "plan", "op": "eq", "value": "enterprise"}
      ]
    }
  }' | python3 -m json.tool

NOT example — exclude free users

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

Deeply nested (AND inside OR inside AND)

{
  "AND": [
    {"field": "active", "op": "eq", "value": true},
    {
      "OR": [
        {"field": "region", "op": "eq", "value": "US"},
        {
          "AND": [
            {"field": "region", "op": "eq", "value": "EU"},
            {"field": "gdpr_consented", "op": "eq", "value": true}
          ]
        }
      ]
    }
  ]
}

Challenge

Write 20 records with type, status, and amount fields. Build a query that finds: type = "order" AND (status = "paid" OR amount > 500).

---

← Previous
Lesson 12: All 15 WhereOp Operators
Next →
Lesson 14: Sort Order & Cursor Pagination