← All lessons/Graph & Joins
27
Graph & Joins

Multi-Agent Joins

Inner-join two agents on a shared field in a single SaQL query.

Prerequisite: Lessons 4 and 11 complete

What you'll learn

  • type: join with agents array and on field
  • Inner join semantics — only rows where on-field matches in BOTH agents
  • Users without orders excluded; orders without users excluded
  • Join response: {join_results: [{key, agent1: {}, agent2: {}}]}
  • Requires ≥2 agents in the agents array
Challenge

Join products and reviews on product_id. Write 3 products, 5 reviews (one product has no review). Confirm unreviewed product is excluded.

Interactive Walkthrough

What you'll learn

Inner-join two agents on a shared field in a single SaQL query.

Join query shape

{
  "type":   "join",
  "agents": ["users", "orders"],
  "on":     "user_id"
}

Both agents must have records with the user_id field. The join returns matched pairs.

Setup — write records to two agents with shared user_id

`bash # Users curl -s -X POST http://localhost:7475/v1/agents/users/write \ -H "Content-Type: application/json" \ -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \ -d '{"payload": {"user_id": "usr_001", "name": "Alice", "plan": "pro"}}' \ | python3 -m json.tool

# Orders curl -s -X POST http://localhost:7475/v1/agents/orders/write \ -H "Content-Type: application/json" \ -H "Authorization: Bearer spx_root_YOUR_ROOT_KEY" \ -d '{"payload": {"user_id": "usr_001", "amount": 299.99, "status": "paid"}}' \ | python3 -m json.tool `

Execute the join

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":   "join",
    "agents": ["users", "orders"],
    "on":     "user_id"
  }' | python3 -m json.tool

Response: `json { "join_results": [ { "key": "usr_001", "users": {"user_id": "usr_001", "name": "Alice", "plan": "pro"}, "orders": {"user_id": "usr_001", "amount": 299.99, "status": "paid"} } ] } `

Join semantics

  • Inner join: only returns rows where user_id exists in BOTH agents
  • Users without orders: excluded
  • Orders without matching users: excluded

Challenge

Set up a join between products and reviews on product_id. Write 3 products and 5 reviews (some products have multiple reviews, one has none). Run the join and confirm the unreviewed product is excluded.

---

← Previous
Lesson 26: Graph Traversal
Next →
Lesson 28: Field Projection