October 01, 2019

#Neo4j Part 6:Interview Questions & Answers (Match, Optional Match, Where and Count clause)

Explain Match clause of Neo4j.
Get All Nodes Using Match: With Match clause we can retrieve all nodes in the Neo4j database. Query to return all the nodes in Neo4j database.
MATCH (n) RETURN n 

Getting All Nodes Under a Specific Label: We can get all the nodes under a specific label.
MATCH (node:label) RETURN node 

Match by Relationship: We can retrieve nodes based on relationship using the MATCH clause.
MATCH (node:label)<- i="" n="" nbsp="" relationship="" return="">

Delete All Nodes: We can delete all the nodes using the MATCH clause.
MATCH (n) detach delete n 

Explain Optional Match clause of Neo4j.
It is used to search for the pattern described in it, while using nulls for missing parts of the pattern. The only difference OPTIONAL MATCH and a MATCH clause is that it returns null as a result of the missing parts of the pattern.

MATCH (node:label {properties. . . . . . . . . . . . . .}) 
OPTIONAL MATCH (node)-->(x) 
RETURN x

e.g:
MATCH (a:Employee {name:"himanshu shukla"}) 
OPTIONAL MATCH (a)-->(x) 
RETURN x;

Explain Where clause of Neo4j.
Neo4j CQL's where clause is similar to SQL's where, and is used to filter the results of a MATCH Query.

MATCH (label)  
WHERE label.country = "property" 
RETURN label 

e.g:
MATCH (Employee)  
WHERE Employee.name = "himanshu shukla" 
RETURN Employee 

WHERE Clause with Multiple Conditions
e.g:
MATCH (Employee)  
WHERE Employee.name = "himanshu shukla" AND Employee.id="1"
RETURN Employee 

Using Relationship with Where Clause: We can also use Where clause to filter the nodes using the relationships.

MATCH (n) 
WHERE (n)-[: WORKS_IN]->( {name:"development center"}) 
RETURN n 

Explain Count clause of Neo4j.
The count() function is used to count the number of rows.
MATCH (n { name: 'A' })-->(x) 
RETURN n, count(*)

e.g:
MATCH (n{name:"himanshu shukla"})-->(x)
RETURN count(*)

Explain Group Count clause of Neo4j.
The count() clause is also used to count the groups of relationship types.

e.g: Below Cypher Query which counts and returns the number of nodes participating in each relation.
MATCH (n{name:"himanshu shukla"})-[r]-(x)
RETURN type (r), count(*)

-K Himaanshu Shuklaa..

No comments:

Post a Comment