- Neo4j Tutorial
- Neo4j - Home
- Neo4j - Overview
- Neo4j - Data Model
- Neo4j - Environment Setup
- Neo4j - Building Blocks
- Neo4j CQL Write Clauses
- Neo4j - Merge Command
- Neo4j - Set Clause
- Neo4j - Delete Clause
- Neo4j - Remove Clause
- Neo4j - Foreach Clause
- Neo4j CQL Read Clause
- Neo4j - Match Clause
- Neo4j - Optional Match Clause
- Neo4j - Where Clause
- Neo4j - Count Function
- Neo4j CQL General Clauses
- Neo4j - Return Clause
- Neo4j - Order By Clause
- Neo4j - Limit Clause
- Neo4j - Skip Clause
- Neo4j - With Clause
- Neo4j - Unwind Clause
- Neo4j CQL Functions
- Neo4j - String Functions
- Neo4j - Aggregation Function
- Neo4j CQL Admin
- Neo4j - Backup & Restore
- Neo4j - Index
- Neo4j - Create Unique Constraint
- Neo4j - Drop Unique
- Neo4j Useful Resources
- Neo4j - Quick Guide
- Neo4j - Useful Resources
- Neo4j - Discussion
Neo4j CQL - Sorting
Neo4j CQL ORDER BY clause
Neo4j CQL has provided "ORDER BY" Clause in MATCH Command to sort the results returned by a MATCH query.
We can sort rows either ascending order or descending order.
By default, it sorts rows in ascending order. If we want to sort them in descending order, we need to use DESC clause
ORDER BY clause syntax
ORDER BY <property-name-list> [DESC]
S.No. | Syntax Element | Description |
---|---|---|
1. | ORDER BY | It is a Neo4j CQL keyword. |
2. | <property-name-list> | It is a list of properties used in sorting. |
3. | DESC | It is a Neo4j CQL keyword used to specify descending order.It is optional. |
<property-name-list> syntax:
<node-label-name>.<property1-name>, <node-label-name>.<property2-name>, .... <node-label-name>.<propertyn-name>
Syntax Description
S.No. | Syntax Element | Description |
---|---|---|
1. | <node-label-name> | It is a label name of a Node. |
2. | <property-name> | It is a property name of a Node. |
NOTE -
We should use comma(,) operator to separate the property names list.
Example
This example demonstrates how to use sort results in Ascending order by Employee name.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno
Step 3 - Click on "Execute" button and observe the results.
It returns total number of results available in the database: 4 records
Step 4 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name
Step 5 - Click on "Execute" button and observe the results.
If we observe the results, now records are sorted by Employee.name in ascending order.
Example
This example demonstrates how to use sort results in Descending order by Employee name.
Step 1 - Open Neo4j Data Browser
Step 2 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno
Step 3 - Click on "Execute" button and observe the results.
It returns total number of results available in the database: 4 records
Step 4 - Type the below command on Data Browser
MATCH (emp:Employee) RETURN emp.empid,emp.name,emp.salary,emp.deptno ORDER BY emp.name DESC
Step 5 - Click on "Execute" button and observe the results.
If we observe the results, now records are sorted by Employee.name in Descending order.