liveBook · Manning

Count

As explained in Graph Algorithms for Data Science

The Cypher count function returns the number of rows or the number of non-null values supplied to it.

Use `count()` to turn matched graph patterns into totals. `count(*)` returns the number of rows produced by a query, while `count(variableOrExpression)` returns only non-null values. This makes the function useful for counting nodes, relationships, matches, and populated properties. section 4.2

Check property completeness

To compare User records with available registration dates, count all matched rows and then count the `registeredAt` values. A difference between the totals reveals missing data: 3,594 User nodes are matched, but 3,518 have a non-null registration date. Cast one operand with `toFloat()` before dividing to calculate a percentage rather than an integer result. section 4.2

Listing 4.3 Counting the number of User nodes and their non-null values of registeredAt properties
MATCH (u:User)
RETURN count(*) AS numberOfRows,
      count(u.registeredAt) AS numberOfUsersWithRegisteredAtDate

This query puts row counting and non-null property counting side by side, making the distinction visible in one result. section 4.2

Count what the query actually produces, not what the pattern seems to represent. A match of Tweet-to-User `MENTIONS` relationships produces one row per occurrence, so `count(u)` counts mentions rather than unique mentioned users. Use `count(distinct u)` when each user should contribute only once. Query cardinality—the number of incoming rows—also affects both the result and performance. section 4.3

Use aggregation placement deliberately in multi-step queries:

  • Aggregate with `WITH count(...)` after a `MATCH` when a later match should run once instead of once per earlier row. section 4.5
  • Count directly after each `OPTIONAL MATCH` when collecting separate categories, preventing intermediate rows from multiplying the final totals. section 4.5
  • Count matched hashtag pairs to create a co-occurrence weight; filter duplicate pair orderings with `id(h1) < id(h2)` before ranking results. section 7.2
  • Aggregate component members first, filter the component sizes, then count the remaining components. section 7.3.2

Count co-occurring hashtags

Match two tags attached to the same tweet, count the matches for each pair, sort by that count, and keep the top five. The resulting count becomes the relationship weight in a co-occurrence network; for example, `#ai` and `#nlp` co-occur 1,507 times. section 7.2

How Graph Algorithms for Data Science covers it

How deep it goes
Explains row and non-null semantics, distinct counting, cardinality effects, and staged aggregation without detailing function internals.
Examples
Demonstrates User registration-date completeness, mentioned-user totals, sequential tweet counts, hashtag pairs, and weakly connected component sizes.
What it assumes
Introduces counting through familiar aggregation queries while assuming only basic Cypher pattern matching.
What you can do after
Apply it via Cypher to distinguish rows from non-null values, deduplicate totals, and control intermediate cardinality.
How it connects
Connects `count()` to MATCH, WITH, OPTIONAL MATCH, DISTINCT, cardinality, EXISTS, and co-occurrence networks.

Also explained in

Compare how 5 books explain count →

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.