Subquery Expressions

EXISTS Subqueries

Test whether a pattern exists without returning the matched data.

-- People who know at least one other person named Alice
MATCH (n:Person)
WHERE EXISTS { (n)-[:FRIEND]->(:Person {name: "Alice"}) }
RETURN n.name

-- Negated existence
MATCH (n:Person)
WHERE NOT EXISTS { (n)-[:WORKS_AT]->(:Company) }
RETURN n.name AS unemployed

COUNT Subqueries

Count pattern occurrences inline without a separate WITH stage.

-- Inline count in RETURN
MATCH (n:Person)
RETURN n.name, count{ (n)-[:FRIEND]->() } AS friendCount

-- In WITH for filtering
MATCH (n:Person)
WITH n, count{ (n)-[:FRIEND]->() } AS friendCount
WHERE friendCount > 3
RETURN n.name, friendCount