Neo4j: Clauses Cheat Sheet

Reading clauses

ClauseQueryDescription
MATCHMATCH (n) RETURN nSpecify the patterns to search for in the database.
OPTIONAL MATCHMATCH (node:label {properties. . . . . . . . . . . . . .}) OPTIONAL MATCH (node)-->(x) RETURN xSpecify the patterns to search for in the database while using nulls for missing parts of the pattern.

Projecting clauses

ClauseQueryDescription
RETURN …​ [AS]MATCH (n { name: 'A' })
RETURN n
Defines what to include in the query result set.
WITH …​ [AS]MATCH (n) WITH n ORDER BY n.property RETURN collect(n.property)Allows query parts to be chained together, piping the results from one to be used as starting points or criteria in the next.
UNWIND …​ [AS]UNWIND [a, b, c, d] AS x RETURN xExpands a list into a sequence of rows.

Reading sub-clauses

Sub-clauseQueryDescription
WHEREMATCH (label) WHERE label.country = "property" RETURN labelAdds constraints to the patterns in a MATCH or OPTIONAL MATCH clause or filters the results of a WITH clause.
ORDER BY [ASC[ENDING] | DESC[ENDING]]MATCH (n) RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
A sub-clause following RETURN or WITH, specifying that the output should be sorted in either ascending (the default) or descending order.
SKIPMATCH (n) RETURN n.name, n.runs
ORDER BY n.runs DESC
SKIP numberofrows
Defines from which row to start including the rows in the output.
LIMITMATCH (n) RETURN n ORDER BY n.name
LIMIT numberofrows
Constrains the number of rows in the output.

Reading hints

HintDescription
USING INDEXIndex hints are used to specify which index, if any, the planner should use as a starting point.
USING INDEX SEEKIndex seek hint instructs the planner to use an index seek for this clause.
USING SCANScan hints are used to force the planner to do a label scan (followed by a filtering operation) instead of using an index.
USING JOINJoin hints are used to enforce a join operation at specified points.

Writing clauses

ClauseQueryDescription
CREATECREATE (node_name);
or
CREATE (node1),(node2)
Create nodes and relationships.
DELETEMATCH (n) DELETE n
Delete nodes, relationships or paths. Any node to be deleted must also have all associated relationships explicitly deleted.
DETACH DELETEMATCH (n) DETACH DELETE nDelete a node or set of nodes. All associated relationships will automatically be deleted.
SETMATCH (node:label{properties . . . . . . . . . . . . . . })
SET node.property = value
RETURN node
Update labels on nodes and properties on nodes and relationships.
REMOVEMATCH (node:label{properties . . . . . . . })
REMOVE node.property RETURN node
Remove properties and labels from nodes and relationships.
FOREACHMATCH p = (start node)-[*]->(end node)
WHERE start.node = "node_name" AND end.node = "node_name"
FOREACH (n IN nodes(p)| SET n.marked = TRUE)
Update data within a list, whether components of a path, or the result of aggregation.

Reading/Writing clauses

ClauseQueryDescription
MERGEMERGE (node: label {properties . . . . . . . })Ensures that a pattern exists in the graph. Either the pattern already exists, or it needs to be created.
— ON CREATEMERGE (node:label {properties . . . . . . . . . . .})
ON CREATE SET property.isCreated ="true"
Used in conjunction with MERGE, this write sub-clause specifies the actions to take if the pattern needs to be created.
— ON MATCHMERGE (node:label {properties . . . . . . . . . . .})
ON MATCH SET property.isFound ="true"
Used in conjunction with MERGE, this write sub-clause specifies the actions to take if the pattern already exists.
CALL […​YIELD]CALL dbms.procedures() YIELD name, signature, description as text
WHERE name STARTS WITH 'db.'
RETURN * ORDER BY name ASC
Invokes a procedure deployed in the database and return any results.

Set operations

ClauseQueryDescription
UNION<MATCH Command1>
UNION

<MATCH Command2>
Combines the result of multiple queries into a single result set. Duplicates are removed.
UNION ALL<MATCH Command1> UNION ALL
<MATCH Command2>
Combines the result of multiple queries into a single result set. Duplicates are retained.

Subquery clauses

ClauseDescription
CALL { …​ }Evaluates a subquery, typically used for post-union processing or aggregations.

Multiple graphs

ClauseQueryDescription
USEUSE <graph> <other clauses>Determines which graph a query, or query part, is executed against.

Leave a Reply

Your email address will not be published. Required fields are marked *