Find a node with the maximum value in a specific property using a Cypher query

You can write Cypher queries in different ways to find a node in the Neo4j graph with the maximum value in a specific property. You can use the MAX() function or ORDER BY and LIMIT clauses.

In the following Cypher query examples, I am trying to find the Disease node with the maximum node_id.

Using the MAX() function:

MATCH (d:Disease) 
WITH MAX(d.node_id) as max_node 
MATCH (d1:Disease) 
WHERE d1.node_id=max_node 
RETURN d1.node_id, d1.node_label

Using the ORDER BY and LIMIT clauses:

MATCH (d:Disease) 
RETURN d.node_id, d.node_label 
ORDER BY d.node_id DESC LIMIT 1

or

MATCH (d:Disease) 
WITH d ORDER BY d.node_id DESC LIMIT 1 
RETURN d.node_id, d.node_label

To use the above queries for your graph, you need to replace the node label (Disease) and property name (node_id).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.