Neo4j Reading Data

UNION

MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION
MATCH (a)-[:LOVES]->(b)
RETURN b.name

Returns the distinct union of all query results. Result column types and names have to match.

MATCH (a)-[:KNOWS]->(b)
RETURN b.name
UNION ALL
MATCH (a)-[:LOVES]->(b)
RETURN b.name

Returns the union of all query results, including duplicated rows.

WITH

MATCH (user)-[:FRIEND]-(friend)
WHERE user.name = $name
WITH user, count(friend) AS friends
WHERE friends > 10
RETURN user

The WITH syntax is similar to RETURN. It separates query parts explicitly, allowing you to declare which variables to carry over to the next part.

MATCH (user)-[:FRIEND]-(friend)
WITH user, count(friend) AS friends
ORDER BY friends DESC
  SKIP 1
  LIMIT 3
RETURN user

ORDER BY, SKIP, and LIMIT can also be used with WITH.

RETURN

RETURN *

Return the value of all variables.

RETURN n AS columnName

Use alias for result column name.

RETURN DISTINCT n

Return unique rows.

ORDER BY n.property

Sort the result.

ORDER BY n.property DESC

Sort the result in descending order.

SKIP $skipNumber

Skip a number of results.

LIMIT $limitNumber

Limit the number of results.

SKIP $skipNumber LIMIT $limitNumber

Skip results at the top and limit the number of results.

RETURN count(*)

The number of matching rows. See Aggregating functions for more.

WHERE

WHERE n.property <> $value

Use a predicate to filter. Note that WHERE is always part of a MATCH, OPTIONAL MATCH or WITH clause. Putting it after a different clause in a query will alter what it does.

WHERE EXISTS {
  MATCH (n)-->(m) WHERE n.age = m.age
}

Use an existential subquery to filter.

MATCH

MATCH (n:Person)-[:KNOWS]->(m:Person)
WHERE n.name = 'Alice'

Node patterns can contain labels and properties.

MATCH (n)-->(m)

Any pattern can be used in MATCH.

MATCH (n {name: 'Alice'})-->(m)

Patterns with node properties.

MATCH p = (n)-->(m)

Assign a path to p.

OPTIONAL MATCH (n)-[r]->(m)

Optional pattern: nulls will be used for missing parts.

Comments