Ctrl K
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:
cypher
CREATE (alice:Person {
name: "Alice Johnson",
age: 30,
email: "alice@example.com"
})
RETURN aliceWhat this does:
CREATE- Creates a new node(alice:Person ...)- Variable namealice, labelPerson{name: ..., age: ...}- Properties (see Property Data Types for all supported types)RETURN alice- Returns the created node
2⃣ Creating Multiple Nodes
cypher
CREATE
(bob:Person {name: "Bob Smith", age: 35}),
(carol:Person {name: "Carol White", age: 28}),
(company:Company {name: "TechCorp", founded: 2010})
RETURN bob, carol, company3⃣ Creating Relationships
Connect Alice to the company:
cypher
MATCH
(alice:Person {name: "Alice Johnson"}),
(company:Company {name: "TechCorp"})
CREATE (alice)-[r:WORKS_AT {since: 2020, role: "Engineer"}]->(company)
RETURN alice, r, companyRelationship syntax:
(alice)-[r:WORKS_AT {...}]->(company)- Directed relationshipr:WORKS_AT- Relationship type{since: 2020, role: "Engineer"}- Relationship properties
4⃣ Querying Data
Find all people
cypher
MATCH (p:Person)
RETURN p.name, p.age
ORDER BY p.age DESCFind relationships
cypher
MATCH (p:Person)-[r:WORKS_AT]->(c:Company)
RETURN p.name, r.role, c.nameFilter with WHERE
cypher
MATCH (p:Person)
WHERE p.age > 30
RETURN p.name, p.agePattern matching
cypher
// 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.name5⃣ Updating Data
Update properties
cypher
MATCH (alice:Person {name: "Alice Johnson"})
SET alice.age = 31, alice.city = "San Francisco"
RETURN aliceAdd labels
cypher
MATCH (alice:Person {name: "Alice Johnson"})
SET alice:Employee:Manager
RETURN labels(alice)Remove properties
cypher
MATCH (alice:Person {name: "Alice Johnson"})
REMOVE alice.email
RETURN alice6⃣ Deleting Data
Delete a node (must delete relationships first)
cypher
MATCH (bob:Person {name: "Bob Smith"})
DETACH DELETE bobNote: DETACH DELETE removes the node and all its relationships.
Delete specific relationships
cypher
MATCH (alice:Person)-[r:WORKS_AT]->()
DELETE rDelete all data ( Use with caution!)
cypher
MATCH (n)
DETACH DELETE n7⃣ Common Patterns
Create if not exists (MERGE)
cypher
MERGE (alice:Person {name: "Alice Johnson"})
ON CREATE SET alice.created = timestamp()
ON MATCH SET alice.accessed = timestamp()
RETURN aliceCount nodes
cypher
MATCH (p:Person)
RETURN count(p) as totalPeopleAggregate data
cypher
MATCH (p:Person)
RETURN
count(p) as total,
avg(p.age) as averageAge,
min(p.age) as youngest,
max(p.age) as oldestCollect into list
cypher
MATCH (p:Person)
RETURN collect(p.name) as allNames8⃣ Practical Example: Social Network
Let's build a small social network:
cypher
// 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
cypher
MATCH (alice:Person {name: "Alice"})-[:FRIENDS_WITH*2]-(fof:Person)
WHERE alice <> fof
RETURN DISTINCT fof.name as friendOfFriendFind shortest path
cypher
MATCH path = shortestPath(
(alice:Person {name: "Alice"})-[:FRIENDS_WITH*]-(dave:Person {name: "Dave"})
)
RETURN path9⃣ Working with Properties
JSON-like properties
cypher
CREATE (doc:Document {
title: "README",
metadata: {
author: "Alice",
version: "1.0",
tags: ["documentation", "guide"]
}
})
RETURN docList properties
cypher
CREATE (person:Person {
name: "Alice",
skills: ["Python", "Go", "JavaScript"],
languages: ["English", "Spanish"]
})
RETURN personAccess nested properties
cypher
MATCH (doc:Document)
RETURN doc.metadata.author, doc.metadata.tagsTips & Best Practices
1. Use parameters for dynamic values
cypher
// Instead of:
MATCH (p:Person {name: "Alice"})
// Use parameters:
MATCH (p:Person {name: $name})2. Create indexes for better performance
cypher
CREATE INDEX person_name FOR (p:Person) ON (p.name)3. Use EXPLAIN to understand query plans
cypher
EXPLAIN MATCH (p:Person) WHERE p.age > 30 RETURN p4. Use PROFILE for performance analysis
cypher
PROFILE MATCH (p:Person)-[:WORKS_AT]->(c:Company) RETURN p, c5. Limit results for large datasets
cypher
MATCH (p:Person)
RETURN p
LIMIT 10Common Functions
String functions
cypher
MATCH (p:Person)
RETURN
toLower(p.name) as lowercase,
toUpper(p.name) as uppercase,
substring(p.name, 0, 3) as first3charsMath functions
cypher
RETURN
abs(-5) as absolute,
round(3.14159, 2) as rounded,
sqrt(16) as squareRootList functions
cypher
RETURN
size([1,2,3,4,5]) as listSize,
head([1,2,3]) as firstElement,
tail([1,2,3]) as restOfListNext 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 ExamplesReady for advanced features? → User Guides