Neo4j Cypher Query Cheatsheet

Created by Thomas Roccia

🗃️ Neo4j is a highly scalable native graph database. Cypher is its declarative query language for expressing graph patterns and operations.

🔍

Basic Graph Exploration

MATCH (n)-[r]->(m) RETURN n, r, m LIMIT 100
Show all nodes, relationships, and connections (limited to 100)
MATCH (n) RETURN n LIMIT 25
Show all nodes in the database (limited to 25)
MATCH ()-[r]->() RETURN r LIMIT 50
Show all relationships (limited to 50)
MATCH (n) RETURN DISTINCT labels(n) as node_labels
Get all unique node labels in the database
MATCH ()-[r]->() RETURN DISTINCT type(r) as relationship_types
Get all unique relationship types
MATCH (n) RETURN count(n) as total_nodes
Count total number of nodes
MATCH ()-[r]->() RETURN count(r) as total_relationships
Count total number of relationships

Basic Node Operations

CREATE (n:Person {name: 'John', age: 30})
Create a node with label 'Person' and properties
MATCH (n:Person) WHERE n.name = 'John' RETURN n
Find nodes by property value
MATCH (n:Person {name: 'John'}) SET n.age = 31 RETURN n
Update node properties
MATCH (n:Person {name: 'John'}) DELETE n
Delete a node (only if no relationships exist)
MATCH (n:Person {name: 'John'}) DETACH DELETE n
Delete a node and all its relationships
🔗

Relationship Operations

CREATE (a:Person {name: 'Alice'})-[r:WORKS_AT {since: 2020}]->(b:Company {name: 'TechCorp'})
Create nodes and relationship in one query
MATCH (a)-[r:KNOWS]->(b) WHERE a.name = 'John' RETURN a, r, b
Find relationships and connected nodes
MATCH (a)-[r:KNOWS]->(b) WHERE a.name = 'John' DELETE r
Delete a specific relationship
🎯

Advanced Queries

MATCH path = shortestPath((a:Person {name: 'John'})-[*]-(b:Person {name: 'Jane'})) RETURN path
Find shortest path between two nodes
MATCH (p:Person) WITH p, [(p)-[:KNOWS]->(friend) | friend.name] as friends RETURN p.name, friends
Collect friends as a list for each person
MATCH (p:Person) OPTIONAL MATCH (p)-[:WORKS_AT]->(c:Company) RETURN p.name, c.name
LEFT JOIN equivalent - return people with or without companies
🗑️

Delete All Data

⚠️ DANGER ZONE
The following queries will permanently delete data. Use with extreme caution!
MATCH (n) DETACH DELETE n
🔥 Delete ALL nodes and relationships in the database (PERMANENT!)
MATCH (n) DETACH DELETE n
🔥 Alternative single-line version - deletes everything (PERMANENT!)
⚙️

Configuration Settings

Key Neo4j configuration settings (neo4j.conf file):

# neo4j.conf - Key Configuration Settings # Memory Settings dbms.memory.heap.initial_size=1G dbms.memory.heap.max_size=1G dbms.memory.pagecache.size=1G # Network Settings dbms.default_listen_address=0.0.0.0 dbms.default_advertised_address=localhost dbms.connector.bolt.listen_address=:7687 dbms.connector.http.listen_address=:7474 # Security Settings dbms.security.auth_enabled=true dbms.security.procedures.unrestricted=gds.*,apoc.* # Performance Settings dbms.query_cache_size=1000 dbms.query.max_time=60s

Memory Tuning Tips:

  • Set heap size to 1/4 of available RAM
  • Page cache should be 1/2 of available RAM (minus heap)
  • Monitor with :sysinfo in Neo4j Browser