TuringDB home pagelight logodark logo
  • Discord
  • GitHub
  • Website
TuringDB

Get Started

  • Introduction
  • Get Started
  • Claude Code Skill
  • Commands

Concepts

  • Overview
  • A Columnar Graph Database
  • Versioning System
  • The DataPart System
  • Zero-Locking Architecture
  • Snapshots Isolation

Benchmarks

  • Results Summary
  • Technical Report

Query Language

  • Overview
  • Matching
  • Paths
  • Results and Aggregation
  • NEXT and WHEN
  • Subqueries
  • Functions and Types
  • Writing Data
  • Procedures and Commands
  • CheatSheet

Importing data

  • JSONL
  • CSV
  • Neo4j
  • GML
  • Parquet

Graph Development

  • Create Graph
  • Load Graph
  • Load External data
  • Create a Change
  • List Changes
  • Create Nodes and Edges
  • Update Properties
  • Submit a Change
  • Time Travel
  • Graph Examples

Vector Search

  • Vector Search

Graph Algorithms

  • Shortest Path

Tutorials

  • Example Notebooks

Python SDK

  • Get Started
  • Reference

JavaScript SDK

  • Get Started
  • Reference

Release Notes

  • All Releases
  • TuringDB 3.1

Troubleshooting

  • Troubleshooting

  • EXISTS
  • COUNT
  • COLLECT
  • CALL subqueries
Query Language

Subqueries

Use EXISTS, COUNT, COLLECT and CALL subqueries to filter, count, collect and compute per-row results in TuringDB.

These examples use the sample graph from the matching page.

​
EXISTS

EXISTS { ... } tests whether a pattern, or a full MATCH with its own WHERE, has at least one match for the current row. It works in WHERE and as a returned value:

MATCH (p:Person)
WHERE EXISTS { (p)-[:WORKS_AT]->(:Company {name: 'Acme'}) }
RETURN p.name

MATCH (p:Person)
WHERE EXISTS {
  MATCH (p)-[:KNOWS]->(f)
  WHERE f.age > p.age
}
RETURN p.name

MATCH (p:Person)
WHERE NOT EXISTS { (p)-[:KNOWS]->() }
RETURN p.name

MATCH (p:Person)
RETURN p.name, EXISTS { (p)-[:LIKES]->() } AS likesSomething

MATCH (p:Person)
WHERE EXISTS {
  MATCH (p)-[:LIKES]->(x)
  RETURN x
  UNION
  MATCH (p)-[:WORKS_AT]->(x:Company {name: 'Globex'})
  RETURN x
}
RETURN p.name

The full form can hold a UNION, as in the last example, a NEXT chain, or a WHEN conditional.

For a single pattern, the function form exists((p)-[:LIKES]->()) is equivalent to EXISTS { (p)-[:LIKES]->() }. exists() takes a pattern only: test a property with p.name IS NOT NULL.

​
COUNT

COUNT { ... } returns the number of matches for each row:

MATCH (p:Person)
RETURN p.name, COUNT { (p)-[:KNOWS]->() } AS outDegree
ORDER BY outDegree DESC

MATCH (p:Person)
WHERE COUNT {
  MATCH (p)-[:KNOWS]-(x)
  RETURN x
} >= 2
RETURN p.name

MATCH (p:Person)
RETURN p.name, COUNT {
  MATCH (p)-[:KNOWS]->(x)
  RETURN x
  UNION
  MATCH (p)-[:LIKES]->(x)
  RETURN x
} AS links

Like EXISTS, the body of COUNT can hold a UNION, a NEXT chain or a WHEN conditional.

​
COLLECT

COLLECT { ... } runs a query for each row and returns its single column as a list. The list is empty when the query finds nothing:

MATCH (p:Person)
RETURN p.name,
  COLLECT { MATCH (p)-[:KNOWS]->(f) RETURN f.name } AS friends

The body is a full query, so it can filter, deduplicate, sort and limit:

MATCH (p:Person)
RETURN p.name, COLLECT {
  MATCH (p)-[:KNOWS]->(f)
  WHERE f.age > 30
  RETURN f.name
} AS olderFriends

MATCH (c:Company)
RETURN c.name, COLLECT {
  MATCH (p:Person)-[:WORKS_AT]->(c)
  RETURN DISTINCT p.city
} AS cities

MATCH (c:Company)
RETURN c.name, COLLECT {
  MATCH (p:Person)-[:WORKS_AT]->(c)
  RETURN p.name
  ORDER BY p.age DESC LIMIT 1
} AS oldestEmployee

It can also combine queries with UNION, or collect nodes to use later:

MATCH (p:Person)
RETURN p.name, COLLECT {
  MATCH (p)-[:KNOWS]->(f)
  RETURN f.name AS link
  UNION
  MATCH (p)-[:WORKS_AT]->(c)
  RETURN c.name AS link
} AS links

MATCH (p:Person {name: 'Alice'})
WITH COLLECT { MATCH (p)-[:KNOWS]->+(f) RETURN f } AS reachable
RETURN [x IN reachable | x.name] AS names

To collect several values per match, return a map, for example a map projection:

MATCH (p:Person)
RETURN p.name, COLLECT {
  MATCH (p)-[:KNOWS]->(f)
  RETURN f {.name, .age}
} AS friends

A COLLECT is an ordinary list value, so it works anywhere a list does: in WHERE, ORDER BY, UNWIND or SET.

MATCH (p:Person)
WHERE size(COLLECT { MATCH (p)-[:KNOWS]-(f) RETURN f.name }) >= 2
RETURN p.name

MATCH (p:Person {name: 'Bob'})
UNWIND COLLECT { MATCH (p)-[:KNOWS]-(f) RETURN f.name } AS friend
RETURN friend

MATCH (p:Person {name: 'Alice'})
SET p.friendNames =
  COLLECT { MATCH (p)-[:KNOWS]->(f) RETURN f.name }
  • One column. The body returns exactly one column. Two columns are reported: A COLLECT subquery must return exactly one column, not 2. To collect several values per match, return a map as above.
  • A full query. The body is MATCH ... RETURN ..., not a bare pattern. It can hold a UNION, a NEXT chain or a WHEN conditional.

​
CALL subqueries

A CALL { ... } subquery runs once per incoming row. It is the way to compute a per-row top-k or a per-row aggregate. Import the outer variables either in the CALL (p) header or with a WITH p as the first clause of the body:

MATCH (p:Person)
CALL (p) {
  MATCH (p)-[:KNOWS]->(f)
  RETURN f.name AS friend
  ORDER BY f.name LIMIT 1
}
RETURN p.name, friend

MATCH (p:Person)
CALL {
  WITH p
  MATCH (p)-[:KNOWS]->(f)
  RETURN count(f) AS deg
}
RETURN p.name, deg

MATCH (p:Person)
CALL (*) {
  MATCH (p)-[:KNOWS]->(f)
  RETURN count(f) AS deg
}
RETURN p.name, deg

MATCH (p:Person)
OPTIONAL CALL (p) {
  MATCH (p)-[:WORKS_AT]->(c)
  RETURN c.name AS co
}
RETURN p.name, co

CALL {
  MATCH (p:Person)
  RETURN p.name AS nm
  UNION
  MATCH (c:Company)
  RETURN c.name AS nm
}
RETURN nm
ORDER BY nm
  • Importing variables. CALL (p) imports the listed variables, and CALL (*) imports every variable in scope, including values computed in an earlier WITH.
  • Dropped rows. A non-optional CALL drops the outer rows for which its body returns nothing. Use OPTIONAL CALL to keep them with null values.
  • Returning an imported variable. Give it an alias in the CALL body: RETURN p AS me.
  • A CALL body can contain UNION, aggregation, ORDER BY, LIMIT and a NEXT chain, or be a conditional query.
  • Writing. Inside a change, a CALL body can write and return what it wrote, and the branches of a UNION can mix CREATE and MERGE:
UNWIND ['x', 'y', 'x'] AS k
CALL (k) {
  MERGE (t:Tag {name: k})
  RETURN t AS tag
}
RETURN tag.name

CALL {
  CREATE (a:Tag {name: 'p'})
  RETURN a AS t
  UNION
  MERGE (b:Tag {name: 'q'})
  RETURN b AS t
}
RETURN t.name

CALL { } IN TRANSACTIONS and OPTIONAL CALL on a procedure are not yet available.

NEXT and WHENFunctions and Types
githublinkedinyoutubediscord