Get ready for your next interview with our comprehensive question library
Nodes represent entities in your data model (like Person, Product, Company). They can have:
:Person, :Movie):ACTED_IN, :FRIENDS_WITH)(person:Person {name: "Tom Hanks", born: 1956})
-[:ACTED_IN {role: "Forrest"}]->
(movie:Movie {title: "Forrest Gump", released: 1994})
Cypher is Neo4j's declarative query language designed specifically for working with graph data. Key differences from SQL:
Cypher characteristics:
(a)-[:REL]->(b)// Cypher: Find friends of friends
MATCH (me:Person {name: "Alice"})-[:FRIEND]-(friend)-[:FRIEND]-(fof)
RETURN fof.name
// SQL equivalent would require multiple JOINs and be more complex
Creating nodes:
CREATE (p:Person {name: "John", age: 30})
Creating relationships:
// Between existing nodes
MATCH (a:Person {name: "John"}), (b:Person {name: "Jane"})
CREATE (a)-[:KNOWS {since: 2020}]->(b)
// Create nodes and relationship together
CREATE (a:Person {name: "Bob"})-[:WORKS_FOR]->(c:Company {name: "TechCorp"})
Best practices:
CREATE always creates new nodes/relationships, potentially causing duplicates:
CREATE (p:Person {name: "John"}) // Always creates new node
MERGE finds existing patterns or creates them if they don't exist:
MERGE (p:Person {name: "John"}) // Creates only if doesn't exist
MERGE with ON CREATE/MATCH:
MERGE (p:Person {email: "john@example.com"})
ON CREATE SET p.created = timestamp(), p.name = "John"
ON MATCH SET p.lastSeen = timestamp()
MERGE is safer for avoiding duplicates but can be slower than CREATE when you're certain the data doesn't exist.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime