First Cypher Queries
First Cypher Queries
Learn MATCH, RETURN, and WHERE clauses
What You'll Learn
- MATCH - Find patterns in the graph
- RETURN - Specify what to return
- WHERE - Filter results with conditions
- LIMIT - Control result size
# Cell 1 — ParametersUSERNAME = "_FILL_ME_IN_" # Set your email before running# Cell 2 — Connectfrom graph_olap import GraphOLAPClientclient = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provisionfrom notebook_setup import provisionpersonas, conn = provision(USERNAME)analyst = personas["analyst"]admin = personas["admin"]ops = personas["ops"]client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")MATCH and RETURN
Finding nodes and selecting output
Every Cypher query starts with MATCH, which describes the pattern to find
in the graph. RETURN then selects which parts of the match to include in the
result set.
MATCH (c:Customer)RETURN c.idLIMIT 5(c:Customer)binds eachCustomernode to the variablec.c.idreads theidproperty.LIMIT 5caps the output at five rows.
# MATCH all Customer nodes and RETURN their namesresult = conn.query("MATCH (c:Customer) RETURN c.id LIMIT 5")
result.show()Counting Results
Aggregation with count()
count() is an aggregation function that returns the number of matched items.
query_scalar() is a convenience method that returns a single value directly,
perfect for queries that produce one row with one column.
# Count all Customer nodes using query_scalar()total = conn.query_scalar("MATCH (c:Customer) RETURN count(c)")print(f"Total customers: {total}")Ordering Results
Sorting with ORDER BY
ORDER BY sorts results by one or more expressions. Add DESC for descending order.
Combine with LIMIT to get “top N” results.
# Order customer names alphabeticallyresult = conn.query(""" MATCH (c:Customer) RETURN c.id AS name ORDER BY c.id""")
result.show()Displaying Results
Using result.show()
The show() method renders query results as a formatted table inside Jupyter.
It auto-detects whether to display tabular data or a graph visualisation.
# Display a richer result set with show()result = conn.query(""" MATCH (c:Customer) RETURN c.id AS name, c.bk_sectr AS sector, c.acct_stus AS id_type ORDER BY c.id""")
result.show()Key Takeaways
MATCHfinds patterns in the graph;RETURNselects what to outputquery_scalar()returns a single value (ideal forcount())ORDER BYsorts results;LIMITrestricts row countresult.show()renders a formatted table in Jupyter