Cypher query guide
Cypher is a declarative query language for graph databases. Just as you handle a relational database with SQL, you can query and manipulate nodes and relationships in a graph with Cypher using an intuitive pattern grammar.
You can explore ontology data by directly executing Cypher queries in D.Hub's Graph Explorer.
Basic grammar
MATCH — pattern matching
Find data that matches a specific pattern in a graph.
MATCH (n)
RETURN n
LIMIT 25
WHERE — Conditional filtering
Apply conditions to the results found with MATCH.
MATCH (p:Person)
WHERE p.age >= 30
RETURN p.name, p.age
RETURN — Return results
Specifies the type of data to search. You can use an alias (AS).
MATCH (p:Person)
RETURN p.name AS name, p.age AS age
ORDER BY — Sort
Sort results by specific properties.
MATCH (p:Person)
RETURN p.name, p.age
ORDER BY p.age DESC
LIMIT — Limit the number of results
Limit the number of results returned. It is recommended to attach it when searching a large amount of data.
MATCH (n)
RETURN n
LIMIT 100
Node pattern
Nodes are expressed in parentheses (). You can specify labels and properties.
| pattern | Description |
|---|---|
(n) | random node |
(p:Person) | Node with label Person |
(p:Person {name: "Alice"}) | Nodes with specific attribute values |
(n:Person:Employee) | Nodes with multiple labels |
Relationship Pattern
The relationship is expressed by square brackets [] and arrows -> and <-.
| pattern | Description |
|---|---|
(a)-[r]->(b) | relationship from a to b |
(a)<-[r]-(b) | relationship from b to a |
(a)-[r]-(b) | Direction irrelevant |
(a)-[:BOUGHT]->(b) | BOUGHT type relationship |
(a)-[*1..3]->(b) | Variable length relationship for 1 to 3 hop distances |
Frequently used query patterns
1. Check all nodes
MATCH (n)
RETURN n
LIMIT 100
2. Search for nodes with a specific label
MATCH (p:Person)
RETURN p
LIMIT 50
3. Filter by attribute conditions
MATCH (p:Product)
WHERE p.price > 10000 AND p.category = "Electronics"
RETURN p.name, p.price
ORDER BY p.price DESC
4. Exploring connections through relationships
MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, p.name
LIMIT 50
5. Exploring multiple relationship paths
MATCH (a:Person)-[:WORKS_FOR]->(company:Organization)-[:LOCATED_IN]->(city:City)
RETURN a.name, company.name, city.name
6. Find the shortest path
MATCH path = shortestPath(
(a:Person {name: "Alice"})-[*]-(b:Person {name: "Bob"})
)
RETURN path
7. N-hop neighbor lookup
MATCH (start:Person {name: "Alice"})-[*1..2]-(neighbor)
RETURN DISTINCT neighbor
LIMIT 50
8. Aggregation — Counting nodes
MATCH (n)
RETURN labels(n) AS label, count(*) AS count
ORDER BY count DESC
9. Aggregation — statistics by relationship
MATCH ()-[r]->()
RETURN type(r) AS relationship, count(*) AS count
ORDER BY count DESC
10. View top N items by group
MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, count(p) AS purchase_count
ORDER BY purchase_count DESC
LIMIT 10
11. Check for pattern existence
MATCH (p:Person)
WHERE EXISTS {
MATCH (p)-[:PURCHASED]->(:Product {category: "Books"})
}
RETURN p.name
12. Collection and use of lists
MATCH (c:Customer)-[:PURCHASED]->(p:Product)
RETURN c.name, collect(p.name) AS purchased_products
LIMIT 20
Useful built-in functions
| function | Description | Example |
|---|---|---|
count() | Counting | count(n) |
collect() | Collect results into a list | collect(n.name) |
labels() | Look up the label of a node | labels(n) |
type() | Relationship type query | type(r) |
properties() | View all properties as a map | properties(n) |
size() | list size | size(collect(n)) |
coalesce() | Returns replacement value when NULL | coalesce(n.name, "Unknown") |
toUpper() | uppercase conversion | toUpper(n.name) |
toString() | string conversion | toString(n.age) |
date() | create date | date("2025-01-01") |
Tips for using in Graph Explorer
In Graph Explorer, query results can be viewed in three views: Graph, Table, and Text. Graph view is suitable for understanding structure, table view is suitable for data analysis, and text view is suitable for debugging.
- Make it a habit to use
LIMIT: Visualization performance degrades when a large number of nodes are returned. Always addLIMIT. - Use the metadata panel: Click on a label or relationship type on the left panel to immediately view data of that type.
- Expanded Navigation: Double-clicking a node in the graph view additionally searches for connected neighboring nodes.
Queries that query the entire graph, such as MATCH (n) RETURN n, may affect browser performance if there is a lot of data. Be sure to specify LIMIT or add a label condition.