+1 vote
ago in Databases by (17.1k points)
Is there any way to use "Group By" clause in the Cypher query of Neo4j? I did not find this clause in the documentation provided by Neo4j.

1 Answer

+3 votes
ago by (364k points)
selected ago by
 
Best answer

In Neo4j, there is no "GROUP BY" clause like relational database queries. However, you can use aggregation functions along with WITH clauses to achieve the same effect. The WITH clause groups by key and allows you to apply aggregation functions like count(), sum(), avg(), collect(), etc.

Here are some example Cypher queries:

Count value for books_written grouped by author
 

MATCH (a:Author)-[:WROTE]->(b:Book)
WITH a.name AS author, count(b) AS books_written
RETURN author, books_written

Average value for price grouped by category

MATCH (p:Product)
WITH p.category AS category, avg(p.price) AS average_price
RETURN category, average_price

Related questions


...