Neo4j Cypher Cheatsheet - Neo4j Graph Database Command Reference

For developers modeling connected data — friends, dependencies, fraud rings — where a JOIN would be clumsy. Cypher is ASCII-art patterns that read as the shape of the graph you want. By the end you can create nodes and typed relationships, query multi-hop patterns with variable-length paths, enforce uniqueness with constraints and speed lookups with indexes, and aggregate over matched paths.

Databases·40 commands·Last updated 2026-07-21
neo4jGraph Databasescypher

Create Nodes & Relationships 5

CREATE (n:Person {name: "Alice"})
Create a node
CREATE (a:Person)-[:KNOWS]->(b:Person)
Create a relationship
CREATE (a:Person {name: "Alice"})-[:KNOWS {since: 2020}]->(b:Person {name: "Bob"})
Create node & relationship with properties
MERGE (n:Person {name: "Alice"})
Match if exists, else create
MERGE (a)-[:KNOWS]->(b) ON CREATE SET a.created = timestamp()
MERGE with onCreate

Query & Match 9

MATCH (n:Person) RETURN n
Match all Person nodes
MATCH (n:Person {name: "Alice"}) RETURN n
Match by property
MATCH (n:Person) WHERE n.age > 18 RETURN n
WHERE filter
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, b
Match a relationship
MATCH (a:Person)-[:KNOWS*2]->(b:Person) RETURN a, b
2-hop relationship
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person) RETURN a, b
1-3 hop relationship
MATCH (n) OPTIONAL MATCH (n)-[:KNOWS]->(m) RETURN n, m
Optional match (null if no rel)
MATCH path = (a)-[*]->(b) RETURN path
Match a path
MATCH path = shortestPath((a)-[:KNOWS*]->(b)) RETURN path
Shortest path

WITH & Subqueries 5

MATCH (n:Person) WITH n WHERE n.age > 18 RETURN n.name
WITH filter & pass
MATCH (n:Person)-[:KNOWS]->(m) WITH n, count(m) AS friends RETURN n.name, friends
WITH aggregate & pass
UNWIND [1, 2, 3] AS x RETURN x
Unwind array into rows
CALL { MATCH (n:Person) RETURN n ORDER BY n.name LIMIT 5 } RETURN count(*)
CALL subquery
MATCH (n:Person) RETURN n.name AS name UNION MATCH (n:Company) RETURN n.name AS name
UNION merge results

Update & Delete 6

MATCH (n:Person {name: "Alice"}) SET n.age = 30
Update a property
MATCH (n:Person {name: "Alice"}) SET n += {age: 30, city: "Beijing"}
Update multiple props
MATCH (n:Person {name: "Alice"}) REMOVE n.age
Remove a property
MATCH (n:Person {name: "Alice"}) DELETE n
Delete node (must have no rels)
MATCH (n:Person {name: "Alice"}) DETACH DELETE n
Delete node and its relationships
MATCH (a)-[r:KNOWS]->(b) DELETE r
Delete a relationship

Aggregation & Order 9

MATCH (n:Person) RETURN count(n)
Count
MATCH (n:Person) RETURN DISTINCT n.city
Distinct
MATCH (n:Person) RETURN n.name, n.age ORDER BY n.age DESC
Order by
MATCH (n:Person) RETURN n.age, count(*) ORDER BY n.age
Group & count
MATCH (n:Person) RETURN collect(n.name)
Collect into array
MATCH (n:Person) RETURN avg(n.age), min(n.age), max(n.age)
Aggregate functions
MATCH (n:Person) RETURN percentileCont(n.age, 0.5)
Median (percentile)
MATCH (n:Person) RETURN n LIMIT 10
Limit results
MATCH (n:Person) RETURN n SKIP 5 LIMIT 10
Pagination

Index & Constraint 6

CREATE INDEX ON :Person(name)
Create an index
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE
Unique constraint
CREATE CONSTRAINT ON (n:Person) ASSERT exists(n.name)
Existence constraint
DROP INDEX ON :Person(name)
Drop an index
DROP CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUE
Drop a constraint
CALL db.indexes()
List all indexes

Tips

  • Run EXPLAIN before MATCH to avoid full-graph scans.
  • MERGE is safer than CREATE — it avoids duplicating nodes.
  • Long path queries (*1..10) can be slow; add LIMIT or use graph algorithms.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us