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.
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 nMatch all Person nodes
MATCH (n:Person {name: "Alice"}) RETURN nMatch by property
MATCH (n:Person) WHERE n.age > 18 RETURN nWHERE filter
MATCH (a:Person)-[:KNOWS]->(b:Person) RETURN a, bMatch a relationship
MATCH (a:Person)-[:KNOWS*2]->(b:Person) RETURN a, b2-hop relationship
MATCH (a:Person)-[:KNOWS*1..3]->(b:Person) RETURN a, b1-3 hop relationship
MATCH (n) OPTIONAL MATCH (n)-[:KNOWS]->(m) RETURN n, mOptional match (null if no rel)
MATCH path = (a)-[*]->(b) RETURN pathMatch a path
MATCH path = shortestPath((a)-[:KNOWS*]->(b)) RETURN pathShortest path
WITH & Subqueries 5
MATCH (n:Person) WITH n WHERE n.age > 18 RETURN n.nameWITH filter & pass
MATCH (n:Person)-[:KNOWS]->(m) WITH n, count(m) AS friends RETURN n.name, friendsWITH aggregate & pass
UNWIND [1, 2, 3] AS x RETURN xUnwind 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 nameUNION merge results
Update & Delete 6
MATCH (n:Person {name: "Alice"}) SET n.age = 30Update a property
MATCH (n:Person {name: "Alice"}) SET n += {age: 30, city: "Beijing"}Update multiple props
MATCH (n:Person {name: "Alice"}) REMOVE n.ageRemove a property
MATCH (n:Person {name: "Alice"}) DELETE nDelete node (must have no rels)
MATCH (n:Person {name: "Alice"}) DETACH DELETE nDelete node and its relationships
MATCH (a)-[r:KNOWS]->(b) DELETE rDelete a relationship
Aggregation & Order 9
MATCH (n:Person) RETURN count(n)Count
MATCH (n:Person) RETURN DISTINCT n.cityDistinct
MATCH (n:Person) RETURN n.name, n.age ORDER BY n.age DESCOrder by
MATCH (n:Person) RETURN n.age, count(*) ORDER BY n.ageGroup & 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 10Limit results
MATCH (n:Person) RETURN n SKIP 5 LIMIT 10Pagination
Index & Constraint 6
CREATE INDEX ON :Person(name)Create an index
CREATE CONSTRAINT ON (n:Person) ASSERT n.name IS UNIQUEUnique 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 UNIQUEDrop 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