# Quick question on a cypher query to arrange nodes by centrality

**URL:** <https://community.neo4j.com/t/quick-question-on-a-cypher-query-to-arrange-nodes-by-centrality/63402>\
**Category:** Cypher\
**Created:** [July 24, 2023, 5:52am UTC](https://community.neo4j.com/t/quick-question-on-a-cypher-query-to-arrange-nodes-by-centrality/63402 "2023-07-24T05:52:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnnandi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tnnandi/32/28528_2.png) [@tnnandi](https://community.neo4j.com/u/tnnandi)\
**Post date:** [July 24, 2023, 5:52am UTC](https://community.neo4j.com/t/quick-question-on-a-cypher-query-to-arrange-nodes-by-centrality/63402/1 "2023-07-24T05:52:52Z")

</div>

Hi,

I'm working with a graph containing \> 50 types of nodes, and want to i) find all nodes of type "Gene" that are connected to node named "cardiomyopathy" of type "Disease", and ii) arrange the nodes found in order of their degree centrality.

For i) I do the following:

```auto
MATCH (c:Disease {name:"cardiomyopathy"})-[]-(g:Gene)
RETURN g.name AS Gene

```

but I can't figure out how to arrange the Gene nodes obtained above by their degree centrality.

For example, the following doesn't work,

```auto
MATCH (c:Disease {name: "cardiomyopathy"})-[]-(g:Gene)
WITH g, size((g)--()) as degreeCentrality
RETURN g.name AS Gene, degreeCentrality
ORDER BY degreeCentrality DESC

```

and throws an error

```auto
A pattern expression should only be used in order to test the existence of a pattern. It should therefore only be used in contexts that evaluate to a boolean, e.g. inside the function exists() or in a WHERE-clause. No other uses are allowed, instead they should be replaced by a pattern comprehension. (line 2, column 14 (offset: 69))
"WITH g, size((g)--()) as degreeCentrality"

```

I'm using neo4j 5.6 within neo4j desktop. I also have the gds and apoc plugins available.

Thanks,  
Tarak

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [July 24, 2023, 11:41am UTC](https://community.neo4j.com/t/quick-question-on-a-cypher-query-to-arrange-nodes-by-centrality/63402/2 "2023-07-24T11:41:54Z")

</div>

Use the new count subquery

```auto
MATCH (c:Disease {name: "cardiomyopathy"})-[]-(g:Gene)
WITH g, count{(g)--()} as degreeCentrality
RETURN g.name AS Gene, degreeCentrality
ORDER BY degreeCentrality DESC

```

> **[Expressions - Cypher Manual](https://neo4j.com/docs/cypher-manual/current/syntax/expressions/)**
>
> This section contains an overview of expressions in Cypher with examples.

---

<div class="post-metadata">

**Author:** ![tnnandi](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/tnnandi/32/28528_2.png) [@tnnandi](https://community.neo4j.com/u/tnnandi)\
**Post date:** [July 24, 2023, 3:48pm UTC](https://community.neo4j.com/t/quick-question-on-a-cypher-query-to-arrange-nodes-by-centrality/63402/3 "2023-07-24T15:48:29Z")

</div>

Hi @glilienfield, thank you very much! Can you please suggest if the above calculation of degree centrality be done using inbuilt functions or with the graph data science library? I'm interested in more centrality measures, as well as other graph metrics that may be cumbersome to write using basic cypher queries.

For example, what's the easiest way to re-write the cypher query to calculate PageRank for all the relevant nodes?

Thanks,  
Tarak
