Cypher Query Language¶
Cypher is a declarative graph query language that allows for expressive and efficient querying of graph data.
Overview¶
QilbeeDB supports OpenCypher, the industry-standard query language for graph databases. Cypher uses ASCII-art syntax to make queries easy to read and write, using patterns to describe graph structures.
Why Cypher?¶
- Declarative: Describe what you want, not how to get it
- Pattern-based: Use ASCII-art patterns to match graph structures
- Expressive: Complex queries are readable and concise
- Standard: OpenCypher is widely adopted across graph databases
Graph Data Model¶
Cypher works with two fundamental elements:
Nodes¶
Nodes represent entities in your graph:
u- Variable nameUser- Label{name: 'Alice', age: 28}- Properties
Relationships¶
Relationships connect nodes:
[:KNOWS]- Relationship type->- Direction{since: '2020-01-15'}- Properties
Basic Query Structure¶
A typical Cypher query follows this pattern:
Example Query¶
MATCH (u:User)-[:KNOWS]->(friend:User)
WHERE u.name = 'Alice' AND friend.age > 25
RETURN friend.name, friend.age
ORDER BY friend.age DESC
LIMIT 10
This query: 1. MATCH: Finds users named Alice and their friends 2. WHERE: Filters friends older than 25 3. RETURN: Returns friend names and ages 4. ORDER BY: Sorts by age descending 5. LIMIT: Returns only top 10 results
Common Clauses¶
MATCH - Pattern Matching¶
Find nodes and relationships:
MATCH (u:User)
MATCH (u:User)-[:KNOWS]->(f:User)
MATCH (u:User)-[:KNOWS*1..3]->(f) -- Variable-length path
WHERE - Filtering¶
Filter matched patterns:
RETURN - Output¶
Select what to return:
CREATE - Insert Data¶
Create nodes and relationships:
SET - Update Data¶
Update properties:
DELETE - Remove Data¶
Delete nodes and relationships:
ORDER BY - Sorting¶
Sort results:
LIMIT - Restrict Results¶
Limit number of results:
Complete Example¶
Let's build a social network:
Create Data¶
-- Create users
CREATE (alice:User {name: 'Alice', age: 28, city: 'San Francisco'})
CREATE (bob:User {name: 'Bob', age: 32, city: 'New York'})
CREATE (charlie:User {name: 'Charlie', age: 25, city: 'San Francisco'})
-- Create friendships
CREATE (alice)-[:KNOWS {since: '2020-01-15'}]->(bob)
CREATE (alice)-[:KNOWS {since: '2021-03-20'}]->(charlie)
CREATE (bob)-[:KNOWS {since: '2020-06-10'}]->(charlie)
Query Data¶
Find mutual friends:
MATCH (a:User {name: 'Alice'})-[:KNOWS]->(mutual:User)<-[:KNOWS]-(b:User)
WHERE a <> b
RETURN DISTINCT b.name AS mutualFriend
Find friends in same city:
MATCH (u:User {name: 'Alice'})-[:KNOWS]->(friend:User)
WHERE u.city = friend.city
RETURN friend.name, friend.city
Count friends by city:
MATCH (u:User)-[:KNOWS]->(friend:User)
RETURN friend.city, count(*) AS friendsCount
ORDER BY friendsCount DESC
Update Data¶
Update user information:
Delete Data¶
Remove a friendship:
Patterns¶
Simple Pattern¶
Variable-Length Pattern¶
Multiple Patterns¶
Shortest Path¶
MATCH path = shortestPath((a:User)-[:KNOWS*]-(b:User))
WHERE a.name = 'Alice' AND b.name = 'Charlie'
RETURN length(path)
Working with Properties¶
Property Access¶
Property Existence¶
Property Update¶
Aggregations¶
Cypher supports powerful aggregations:
-- Count
RETURN count(u) AS totalUsers
-- Sum
RETURN sum(p.price) AS totalValue
-- Average
RETURN avg(u.age) AS averageAge
-- Collect
RETURN u.name, collect(f.name) AS friends
-- Min/Max
RETURN min(u.age), max(u.age)
Parameters¶
Use parameters for dynamic queries:
Python example:
Best Practices¶
- Use Parameters
- Improves security (prevents injection)
- Enables query plan caching
-
Makes queries reusable
-
Create Indexes
-
Use EXPLAIN
-
Limit Results
- Always use LIMIT for exploration
-
Prevents accidentally loading huge datasets
-
Use Specific Patterns
Common Operations¶
Create Node¶
Find Node¶
Update Node¶
Delete Node¶
Create Relationship¶
Find Relationships¶
Functions¶
Cypher includes many built-in functions:
String Functions¶
Numeric Functions¶
Aggregation Functions¶
Date/Time Functions¶
Resources¶
- MATCH Clause - Pattern matching
- WHERE Clause - Filtering
- RETURN Clause - Output selection
- CREATE Clause - Data insertion
- SET Clause - Data updates
- DELETE Clause - Data deletion
- ORDER BY Clause - Sorting
- LIMIT Clause - Result limiting
- Functions - Built-in functions
Next Steps¶
- Try the Quick Start Guide
- Learn about Graph Operations
- Explore the Python SDK
- Read about Query Optimization