An existential subquery is a Cypher expression that tests whether a specified graph pattern exists at least once. It appears inside a WHERE clause, uses curly brackets, can reuse variables from the outer query, and keeps newly introduced variables inside the subquery. section 4.3
Use existential subqueries to:
- Filter users who participate in at least one matching pattern without expanding every relationship. section 4.3
- Negate a pattern with NOT EXISTS, such as finding mentioned users who have not published a tweet. section 4.3 section 4.7
- Avoid unnecessary row expansion and DISTINCT processing when only existence matters, helping control query cardinality. section 4.3 section 4.7
Find mentioned users
MATCH (n:User) WHERE EXISTS { (n)<-[:MENTIONS]-() } RETURN count(n) AS numberOfDistinctUsers; This counts users mentioned in at least one tweet, rather than counting every MENTIONS relationship occurrence. section 4.3
