October 01, 2019

#Neo4j Part 7:Interview Questions & Answers (Return, Order By, Limit, Skip, With, Unwind clause)

Explain Return clause of Neo4j.
We can return  nodes, relationships, and properties in Neo4j by using Return clause.

Returning Nodes:
Create (node:label {properties}) RETURN node 



e.g:
CREATE (him:Employee {name:"himanshu shukla"} return him;

Returning Multiple Nodes: We can also return multiple nodes, e.g:
CREATE (him:Employee {name:"himanshu shukla"}
CREATE (India: Country {name:"India"}
return him, India;

Returning Relationships: We can also return relationships using the Return clause.
CREATE (node1)-[Relationship:Relationship_type]->(node2) 
RETURN Relationship 

e.g
CREATE (him:Employee {name:"himanshu shukla"}
CREATE (India: Country {name:"India"}
CREATE(him)-[r:STAYS_IN]->(India) 
return r;

Returning Properties: Return clause can be used to return properties as well.
Match (node:label {properties....}) 
Return node.property 

e.g:
MATCH (him:Employee {name:"himanshu shukla"})
return him.name;

Returning All Elements: We can also return all the elements in the Neo4j database using the RETURN clause.
Match m=(n{name:"India"})-[r]-(x)
return *;

Returning a Variable With a Column Alias: 
MATCH (him:Employee {name:"himanshu shukla"})
return him.name as Name Of The Employee;

Explain Order By clause of Neo4j.
With the help of ORDER BY clause, we can arrange the result data in order
MATCH (n)  RETURN n.property1, n.property2 .. 
ORDER BY n.property

e.g:
CREATE (him:Employee {name:"himanshu", age:34});
CREATE (him:Employee {name:"sebastian", age:26});
CREATE (him:Employee {name:"ronaldo", age:29});

Following is a sample Cypher Query which returns the above created nodes in the order of there age using the ORDERBY clause.
Match(Employee) 
RETURN Employee.name, Employee.age
ORDER BY Employee.age

Ordering Nodes by Multiple Properties: We can arrange the nodes based on multiple properties using ORDEYBY clause.
MATCH (n) 
RETURN n 
ORDER BY n.age, n.name 

Ordering Nodes by Descending Order: We can arrange the nodes in a database in a descending order using the ORDERBY clause.
MATCH (n) 
RETURN n 
ORDER BY n.name DESC 

Explain Limit and Skip clause of Neo4j.
We can limit the number of rows in the output by using the limit clause.
MATCH (n) 
RETURN n 
ORDER BY n.name 
LIMIT 3;

e.g:
Match(Employee) 
RETURN Employee.name, Employee.age
LIMIT 2;

It is used to define from which row to start including the rows in the output.
MATCH (n) 
RETURN n 
ORDER BY n.name DESC 
SKIP 3;

What is the purpose of With and UnWind clause of Neo4j?
We can chain the queries together using the WITH clause.

MATCH (n) 
WITH n 
ORDER BY n.name DESC LIMIT 3 
RETURN collect(n.name)

The unwind clause is used to unwind a list into a sequence of rows.

UNWIND [a, b, c, d] AS x 
RETURN x 

-K Himaanshu Shuklaa..

No comments:

Post a Comment