Skip to content

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

  1. Creating nodes and relationships
  2. Querying graph data
  3. Updating properties
  4. Deleting data
  5. Basic graph patterns

1️⃣ Creating Your First Node

Let's create a person node:

CREATE (alice:Person {
  name: "Alice Johnson",
  age: 30,
  email: "alice@example.com"
})
RETURN alice

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

MATCH (p:Person)
RETURN p.name, p.age
ORDER BY p.age DESC

Find relationships

MATCH (p:Person)-[r:WORKS_AT]->(c:Company)
RETURN p.name, r.role, c.name

Filter with WHERE

MATCH (p:Person)
WHERE p.age > 30
RETURN p.name, p.age

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

MATCH (alice:Person {name: "Alice Johnson"})
SET alice:Employee:Manager
RETURN labels(alice)

Remove properties

MATCH (alice:Person {name: "Alice Johnson"})
REMOVE alice.email
RETURN alice

6️⃣ Deleting Data

Delete a node (must delete relationships first)

MATCH (bob:Person {name: "Bob Smith"})
DETACH DELETE bob

Note: DETACH DELETE removes the node and all its relationships.

Delete specific relationships

MATCH (alice:Person)-[r:WORKS_AT]->()
DELETE r

Delete all data (⚠️ Use with caution!)

MATCH (n)
DETACH DELETE n

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

MATCH (p:Person)
RETURN count(p) as totalPeople

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

MATCH (p:Person)
RETURN collect(p.name) as allNames

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

MATCH (doc:Document)
RETURN doc.metadata.author, doc.metadata.tags

🔟 Tips & Best Practices

1. Use parameters for dynamic values

// Instead of:
MATCH (p:Person {name: "Alice"})

// Use parameters:
MATCH (p:Person {name: $name})

2. Create indexes for better performance

CREATE INDEX person_name FOR (p:Person) ON (p.name)

3. Use EXPLAIN to understand query plans

EXPLAIN MATCH (p:Person) WHERE p.age > 30 RETURN p

4. Use PROFILE for performance analysis

PROFILE MATCH (p:Person)-[:WORKS_AT]->(c:Company) RETURN p, c

5. Limit results for large datasets

MATCH (p:Person)
RETURN p
LIMIT 10

📚 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

RETURN 
  abs(-5) as absolute,
  round(3.14159, 2) as rounded,
  sqrt(16) as squareRoot

List functions

RETURN 
  size([1,2,3,4,5]) as listSize,
  head([1,2,3]) as firstElement,
  tail([1,2,3]) as restOfList

⏭️ Next Steps

Now that you know the basics:

  1. Vector Search Guide - Semantic search
  2. Complete Examples - Full applications
  3. Cypher Functions Reference - All functions
  4. 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 DELETE instead of DELETE

"Query too slow"

  • Create indexes on frequently queried properties
  • Use EXPLAIN to analyze query plan
  • Limit result sets with LIMIT

Need more examples?Complete Examples
Ready for advanced features?User Guides