Database Reference
In-Depth Information
6.4. Advanced Cypher
You've learned how to write and execute fairly complex Cypher queries so far, but there's
muchmoretoCypherthanthat.Inthissectionwe'regoingtotakealookatsomeofthead-
vanced features in Cypher, like data aggregation, Cypher functions, and chaining multiple
queries together. Let's start with data aggregation.
6.4.1. Aggregation
Just like GROUP BY in SQL, Cypher supports aggregating functions in queries. Instead of
using a dedicated GROUP BY clause, the grouping key in Cypher is defined as all non-ag-
gregating results of the query. Here's how you could count the number of friends for each
user in the graph:
You're looking at the entire graph in this example, so all nodes are used as starting nodes
. For each user node, you match their direct friends by following one level of the
IS_FRIEND_OF relationship
. The return clause contains one non-aggregating
entry ( user node) and one aggregated function ( count ) . This means that you're us-
ing the user node to group the counts by, getting one result row per user. You can use the
aggregated values to order the entire result set in the order by clause as well
.
In addition to the count function, Cypher supports all the usual aggregation functions
from SQL: SUM for summing numeric values, AVG for calculating averages, and MAX and
MIN for finding the maximum or minimum values of numeric properties.
To find the average age of all John's friends, you can use the following query:
Search WWH ::




Custom Search