liveBook · Manning

Existential subquery

As explained in Graph Algorithms for Data Science

An existential subquery is a Cypher subquery expression that evaluates whether at least one instance of a specified graph pattern exists.

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

How Graph Algorithms for Data Science covers it

How deep it goes
Explains WHERE syntax, variable scope, negation, and cardinality benefits, but does not develop execution-plan internals.
Examples
Demonstrates mentioned users, users without published tweets, and retweet-mention exclusion in a Twitter network.
What it assumes
Assumes a Neo4j database with imported Twitter network data and familiarity with basic Cypher matching and filtering.
What you can do after
Apply it via Cypher to filter graph patterns positively or negatively while avoiding unnecessary relationship expansion.
How it connects
Connects WHERE clauses, graph patterns, MATCH clauses, counting subqueries, DISTINCT, cardinality, and PROFILE.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.