Neo4j CQL - MATCH & RETURN



In Neo4j CQL, We cannot use MATCH or RETURN commands alone, so we should combine those two commands to retrieve data from Database.

Neo4j CQL MATCH + RETURN command is used -

  • To retrieve some properties of a Node
  • To retrieve all properties of a Node
  • To retrieve some properties of Nodes and associated Relationships
  • To retrieve all properties of Nodes and associated relationships

MATCH RETURN command syntax:

MATCH Command
RETURN Command

Syntax Description

Syntax ElementDescription
MATCH CommandIt is Neo4j CQL MATCH command.
RETURN CommandIt is Neo4j CQL RETURN command.

MATCH command syntax:

MATCH 
(
   <node-name>:<label-name>
)

Syntax Description

Syntax ElementDescription
<node-name>It is a node name we are going to create.
<label-name>It is a node label name

Important points -

  • Neo4j Database Server uses this <node-name> to store this node details in Database.As a Neo4j DBA or Developer, we cannot use it to access node details.

  • Neo4j Database Server creates a <label-name> as an alias to internal node name.As a Neo4j DBA or Developer, we should use this label name to access node details.

RETURN command syntax:

RETURN 
   <node-name>.<property1-name>,
   ...
   <node-name>.<propertyn-name>

Syntax Description

Syntax ElementDescription
<node-name>It is a node name we are going to create.
<Property1-name>...<Propertyn-name> Properties are key-value pairs. defines the name of the property which is going to assign to a creating Node

Example

This Example demonstrates How to retrieve some properties(deptno,dname) data of Dept Node from Database.

NOTE - Dept node contains 3 properties :deptno,dname,location. However in this example, we are interested to view only two properties data. Follow the steps given below -

Step 1 - Open Neo4j Data Browser.

Step 2 - Type the below command at dollar prompt in Data Browser.

MATCH (dept: Dept)
RETURN dept.deptno,dept.dname

Here -

  • dept is a node name
  • Here Dept is a node label name
  • deptno is a property name of dept node
  • dname is a property name of dept node
Neo4j CQL Tutorial

Step 3 - Click on Execute button and see the success message in the Data Browser.

Neo4j CQL Tutorial

If you observe the Data Browser message, it shows data about Dept node for two properties : deptno,dname. It returns two nodes(rows) available in the Neo4j Database.

Example

This Example demonstrates How to retrieve All properties(deptno,dname,location) data of Dept Node from Database.

NOTE - Dept node contains 3 properties : deptno, dname, location. Follow the steps given below -

Step 1 - Open Neo4j Data Browser.

Neo4j CQL Tutorial

It is Neo4j Data Browser Homepage

Step 2 - Type the below command at dollar prompt in Data Browser.

MATCH (dept: Dept)
RETURN dept.deptno,dept.dname,dept.location

Here -

  • dept is a node name
  • Here Dept is a node label name
  • deptno is a property name of dept node
  • dname is a property name of dept node
  • location is a property name of dept node
Neo4j CQL Tutorial

Step 3 - Click on Execute button and see the success message in the Data Browser.

Neo4j CQL Tutorial

It returns all properties data of Dept node. As Database contains two nodes with same name "dept:Dept", it returns those two rows when we execute this command.

Example

This Example demonstrates How to retrieve data of Dept Node from Database without specifying its properties.

NOTE - Dept node contains 3 properties : deptno, dname, location. Follow the steps given below -

Step 1 - Open Neo4j Data Browser.

Step 2 - Type the below command at dollar prompt in Data Browser.

MATCH (dept: Dept)
RETURN dept

Here dept is a node name

Here Dept is a node label name

Neo4j CQL Tutorial

Step 3 - Click on Execute button and see the success message in the Data Browser.

Neo4j CQL Tutorial

Here we can observe that two circles with some ids in UI Mode

Id = 3215 shows one node

Id = 25 shows another node

When we execute "RETURN" clause without specifying any properties list like "RETURN dept"

By default it shows results in UI Mode.

Step 4 - Click on Grid View button to view two rows in grid format.

Neo4j CQL Tutorial
Advertisements