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.nameThe 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 linksLike 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 friendsThe 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 oldestEmployeeIt 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 namesTo 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 friendsA 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 aUNION, aNEXTchain or aWHENconditional.
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, andCALL (*)imports every variable in scope, including values computed in an earlierWITH. - Dropped rows. A non-optional
CALLdrops the outer rows for which its body returns nothing. UseOPTIONAL CALLto keep them withnullvalues. - Returning an imported variable. Give it an alias in the
CALLbody:RETURN p AS me. - A
CALLbody can containUNION, aggregation,ORDER BY,LIMITand aNEXTchain, or be a conditional query. - Writing. Inside a change, a
CALLbody can write and return what it wrote, and the branches of aUNIONcan mixCREATEandMERGE:
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.nameCALL { } IN TRANSACTIONS and OPTIONAL CALL on a procedure are not yet available.

