![Neo4j Tutorial](/neo4j/images/neo4j-mini-logo.jpg)
- 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 - Return Clause
The RETURN clause is used return nodes, relationships, and properties in Neo4j. In this chapter, we are going to learn how to −
- Return nodes
- Return multiple nodes
- Return relationships
- Return properties
- Return all elements
- Return a variable with column alias
Returning Nodes
You can return a node using the RETURN clause.
Syntax
Following is a syntax to return nodes using the RETURN clause.
Create (node:label {properties}) RETURN node
Example
Before proceeding with the example, create 3 nodes and 2 relationships as shown below.
Create (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) CREATE (Ind:Country {name: "India", result: "Winners"}) CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013) CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind)
Following is a sample Cypher Query which creates a node named Dhoni and returns it.
Create (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) RETURN Dhoni
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![Return](/neo4j/images/return.jpg)
Result
On executing, you will get the following result.
![Create Player](/neo4j/images/create_player.jpg)
Returning Multiple Nodes
You can also return multiple nodes using the return clause.
Syntax
Following is the syntax to return multiple nodes using the return clause.
CREATE (Ind:Country {name: "India", result: "Winners"}) CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) RETURN Ind, CT2013
Example
Following is a sample Cypher Query to return multiple nodes using the return clause.
CREATE (Ind:Country {name: "India", result: "Winners"}) CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"}) RETURN Ind, CT2013
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![Multi Node](/neo4j/images/multi_node.jpg)
Result
On executing, you will get the following result. Here you can observe that Neo4j returned 2 nodes.
![Create Tornament](/neo4j/images/create_tornament.jpg)
Returning Relationships
You can also return relationships using the Return clause.
Syntax
Following is the syntax to return relationships using the RETURN clause.
CREATE (node1)-[Relationship:Relationship_type]->(node2) RETURN Relationship
Example
Following is a sample Cypher Query which creates two relationships and returns them.
CREATE (Ind)-[r1:WINNERS_OF {NRR:0.938 ,pts:6}]->(CT2013) CREATE(Dhoni)-[r2:CAPTAIN_OF]->(Ind) RETURN r1, r2
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![Relationship Return](/neo4j/images/relationship_return.jpg)
Result
On executing, you will get the following result.
![Create Winners](/neo4j/images/create_winners.jpg)
Returning Properties
You can also return properties using the RETURN clause.
Syntax
Following is a syntax to return properties using the RETURN clause.
Match (node:label {properties . . . . . . . . . . }) Return node.property
Example
Following is a sample Cypher Query to return the properties of a node.
Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) Return Dhoni.name, Dhoni.POB
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![Property Return](/neo4j/images/property_return.jpg)
Result
On executing, you will get the following result.
![Streaming](/neo4j/images/streaming.jpg)
Returning All Elements
You can return all the elements in the Neo4j database using the RETURN clause.
Example
Following is an example Cypher Query to return all the elements in the database.
Match p = (n {name: "India", result: "Winners"})-[r]-(x) RETURN *
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![All Elements](/neo4j/images/all_elements.jpg)
Result
On executing, you will get the following result.
![All Elements Result](/neo4j/images/all_elements_result.jpg)
Returning a Variable With a Column Alias
You can return a particular column with alias using RETURN clause in Neo4j.
Example
Following is a sample Cypher Query which returns the column POB as Place Of Birth.
Match (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"}) Return Dhoni.POB as Place Of Birth
To execute the above query, carry out the following steps −
Step 1 − Open the Neo4j desktop App and start the Neo4j Server. Open the built-in browser app of Neo4j using the URL http://localhost:7474/ as shown in the following screenshot.
![Browser App](/neo4j/images/browser_app.jpg)
Step 2 − Copy and paste the desired query in the dollar prompt and press the play button (to execute the query) highlighted in the following screenshot.
![Return Column](/neo4j/images/return_column.jpg)
Result
On executing, you will get the following result.
![Column Alias](/neo4j/images/column_alias.jpg)
To Continue Learning Please Login