liveBook · Manning

Cypher

As explained in Graph Algorithms for Data Science

Cypher syntax is the grammatical system governing how Cypher queries represent graph elements and operations.

Cypher is a graph query language for labeled-property graphs. Its ASCII-art patterns make nodes, relationships, labels, properties, and directions readable in a compact query form. Use it to create, find, transform, and analyze graph data. section 2.0 section 2.2

Write a node in parentheses. Add a label after a colon and properties as key-value pairs in braces: (:Person {name:"Thomas"}). Give it a variable, such as (thomas:Person), when later parts of the same statement must refer to it. section 3.0

Write a relationship in square brackets between its adjacent nodes: ()-[:RELATIONSHIP]->(). Relationship types follow a colon, properties go in braces, and arrows express direction. Omitting the arrow depicts an undirected pattern, although Neo4j stores relationships as directed. section 2.2 section 3.4

Core clauses support a practical query workflow:

  • CREATE adds graph data; MATCH identifies existing graph patterns. section 3.4
  • WHERE filters matches or intermediate rows; OPTIONAL MATCH preserves other returned variables when a pattern is absent. section 3.4
  • WITH selects, filters, aggregates, paginates, or limits intermediate rows. section 3.4
  • SET and REMOVE change properties or labels; DELETE removes graph elements, while DETACH DELETE removes a connected node. section 3.4
  • MERGE ensures a pattern exists, supporting idempotent imports and automatic deduplication. section 3.4

Visualize follower relationships

Bind a matched path to p, return it, and limit the result to a manageable sample. A path is a sequence of connected nodes and relationships and can be referenced with a variable like nodes and relationships. section 4.1

Listing 4.1 Generated Cypher query that visualizes a sample of 25 FOLLOWS relationships
MATCH p=()-[r :FOLLOWS]->()
RETURN p LIMIT 25;

This query matches FOLLOWS relationships, returns each matched path, and limits the visualization to 25 examples. section 4.1

Count hashtag co-occurrences

Match two Tag nodes attached to the same tweet, use id(h1) < id(h2) to avoid duplicate pairs, count shared tweets, then order and limit the results. The count becomes the relationship weight in a co-occurrence network. section 7.2

Listing 7.9 Examining the top five most co-occurring pairs of hashtags
MATCH (h1:Tag)<-[:HAS_TAG]-()-[:HAS_TAG]->(h2:Tag)
WHERE id(h1) < id(h2)                              #1
WITH h1,h2,count(*) AS cooccurrences
ORDER BY cooccurrences DESC LIMIT 5
RETURN h1.id AS tag1, h2.id AS tag2, cooccurrences 

The listing combines MATCH, WHERE, WITH, count(), ORDER BY, LIMIT, and RETURN to rank hashtag pairs by co-occurrence. section 7.2

How Graph Algorithms for Data Science covers it

How deep it goes
Explains node and relationship notation, variables, major clauses, aggregation, filtering, and pattern-counting queries without optimization details.
Examples
Uses a Thomas–Elaine friendship, sampled Twitter FOLLOWS paths, and ranked co-occurring hashtag pairs.
What it assumes
Teaches graph-pattern syntax after introducing labeled-property graph modeling; Neo4j setup is needed to run the examples.
What you can do after
Apply it via Neo4j to create, retrieve, filter, aggregate, update, and visualize graph patterns.
How it connects
Connects labeled-property graphs with graph modeling, Neo4j, graph patterns, paths, aggregation, and co-occurrence networks.

Also explained in

Compare how 4 books explain Cypher →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Algorithms for Data Science. The section links point to the parts of the book it draws on.