Your First Queries¶
Learn basic Cypher queries to create, read, update, and delete graph data.
📋 Prerequisites¶
- NornicDB installed and running (see Installation)
- Connected to database (HTTP or Bolt)
🎯 What You'll Learn¶
- Creating nodes and relationships
- Querying graph data
- Updating properties
- Deleting data
- Basic graph patterns
1️⃣ Creating Your First Node¶
Let's create a person node:
What this does: - CREATE - Creates a new node - (alice:Person ...) - Variable name alice, label Person - {name: ..., age: ...} - Properties (see Property Data Types for all supported types) - RETURN alice - Returns the created node
2️⃣ Creating Multiple Nodes¶
CREATE
(bob:Person {name: "Bob Smith", age: 35}),
(carol:Person {name: "Carol White", age: 28}),
(company:Company {name: "TechCorp", founded: 2010})
RETURN bob, carol, company
3️⃣ Creating Relationships¶
Connect Alice to the company:
MATCH
(alice:Person {name: "Alice Johnson"}),
(company:Company {name: "TechCorp"})
CREATE (alice)-[r:WORKS_AT {since: 2020, role: "Engineer"}]->(company)
RETURN alice, r, company
Relationship syntax: - (alice)-[r:WORKS_AT {...}]->(company) - Directed relationship - r:WORKS_AT - Relationship type - {since: 2020, role: "Engineer"} - Relationship properties
4️⃣ Querying Data¶
Find all people¶
Find relationships¶
Filter with WHERE¶
Pattern matching¶
// Find people who work at the same company
MATCH (p1:Person)-[:WORKS_AT]->(c:Company)<-[:WORKS_AT]-(p2:Person)
WHERE p1.name < p2.name // Avoid duplicates
RETURN p1.name, p2.name, c.name
5️⃣ Updating Data¶
Update properties¶
MATCH (alice:Person {name: "Alice Johnson"})
SET alice.age = 31, alice.city = "San Francisco"
RETURN alice
Add labels¶
Remove properties¶
6️⃣ Deleting Data¶
Delete a node (must delete relationships first)¶
Note: DETACH DELETE removes the node and all its relationships.
Delete specific relationships¶
Delete all data (⚠️ Use with caution!)¶
7️⃣ Common Patterns¶
Create if not exists (MERGE)¶
MERGE (alice:Person {name: "Alice Johnson"})
ON CREATE SET alice.created = timestamp()
ON MATCH SET alice.accessed = timestamp()
RETURN alice
Count nodes¶
Aggregate data¶
MATCH (p:Person)
RETURN
count(p) as total,
avg(p.age) as averageAge,
min(p.age) as youngest,
max(p.age) as oldest
Collect into list¶
8️⃣ Practical Example: Social Network¶
Let's build a small social network:
// Create people
CREATE
(alice:Person {name: "Alice", age: 30}),
(bob:Person {name: "Bob", age: 35}),
(carol:Person {name: "Carol", age: 28}),
(dave:Person {name: "Dave", age: 32})
// Create friendships
CREATE
(alice)-[:FRIENDS_WITH {since: 2020}]->(bob),
(alice)-[:FRIENDS_WITH {since: 2019}]->(carol),
(bob)-[:FRIENDS_WITH {since: 2021}]->(dave),
(carol)-[:FRIENDS_WITH {since: 2020}]->(dave)
RETURN *
Find friends of friends¶
MATCH (alice:Person {name: "Alice"})-[:FRIENDS_WITH*2]-(fof:Person)
WHERE alice <> fof
RETURN DISTINCT fof.name as friendOfFriend
Find shortest path¶
MATCH path = shortestPath(
(alice:Person {name: "Alice"})-[:FRIENDS_WITH*]-(dave:Person {name: "Dave"})
)
RETURN path
9️⃣ Working with Properties¶
JSON-like properties¶
CREATE (doc:Document {
title: "README",
metadata: {
author: "Alice",
version: "1.0",
tags: ["documentation", "guide"]
}
})
RETURN doc
List properties¶
CREATE (person:Person {
name: "Alice",
skills: ["Python", "Go", "JavaScript"],
languages: ["English", "Spanish"]
})
RETURN person
Access nested properties¶
🔟 Tips & Best Practices¶
1. Use parameters for dynamic values¶
2. Create indexes for better performance¶
3. Use EXPLAIN to understand query plans¶
4. Use PROFILE for performance analysis¶
5. Limit results for large datasets¶
📚 Common Functions¶
String functions¶
MATCH (p:Person)
RETURN
toLower(p.name) as lowercase,
toUpper(p.name) as uppercase,
substring(p.name, 0, 3) as first3chars
Math functions¶
List functions¶
⏭️ Next Steps¶
Now that you know the basics:
- Vector Search Guide - Semantic search
- Complete Examples - Full applications
- Cypher Functions Reference - All functions
- Advanced Topics - K-Means clustering, embeddings, custom functions
🆘 Common Issues¶
"Node not found"¶
- Make sure you created the node first
- Check spelling of labels and properties
"Cannot delete node with relationships"¶
- Use
DETACH DELETEinstead ofDELETE
"Query too slow"¶
- Create indexes on frequently queried properties
- Use
EXPLAINto analyze query plan - Limit result sets with
LIMIT
Need more examples? → Complete Examples
Ready for advanced features? → User Guides