Command |
Syntax |
Example or Description |
Create node |
CREATE (node_name) |
CREATE(my_node1) |
Create multiple nodes in same query |
CREATE (node_name_1), (node_name_2) …. (node_name_N) |
CREATE(my_node2), (my_node3) |
Create node with label |
CREATE (node_name:label) |
CREATE (my_node4:demoNode4) |
Create a node with multiple labels |
CREATE (node_name:label1:label2:. . . . labelN) |
CREATE (my_node5:demoNode5:exampleNode5:helloNode5) |
Create a node with properties |
CREATE (node_name:label { key1: value, key2: value, . . . . . . . . . }) |
CREATE (Ram:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”}) |
Return the created node after creation |
CREATE (node_name:label{properties. . . . }) RETURN node_name |
CREATE (Sailaja:Person{name: “Sailaja”, age: 32, city: “Hyderabad”, female: true}) RETURN Sailaja |
Return multiple nodes after creation |
CREATE (node1) (node2) RETURN node1, node2 |
CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true}), (MuraliKrishna:Person{name: “Murali Krishna”, age: 34, city: “Hyderabad”, male: true}) RETURN RamaKrishna, MuraliKrishna |
Create relationship |
CREATE (NODE_1)-[:RELATIONSHIP_TYPE]->(NODE_2) |
CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true}) CREATE (Suhana:Person{name: “Suhana Krishna”, age: 2, city: “Bangalore”, female: true}) CREATE (RamaKrishna)-[:FATHER_OF]->(Suhana) |
Create self-relationship |
CREATE (node)-[:Relationship]->(node) |
CREATE (elephant:Animal{name: “Ganesha”})-[:BIG_ANIMAL]->(elephant)
|
Add label, properties to a relationship |
CREATE (node1)-[label:Rel_Type {key1:value1, key2:value2, . . . n}]-> (node2)
|
CREATE (RamaKrishna:Person{name: “Rama Krishna”, age: 32, city: “Bangalore”, male: true}) CREATE (MuraliKrishna:Person{name: “Murali Krishna”, age: 34, city: “Hyderabad”, male: true}) CREATE (RamaKrishna)-[c:COLLEAGUES {since: “5 yrs”, organization: “yahoo”}]->(MuraliKrishna)
|
Get all the nodes |
MATCH (n) RETURN n |
|
Delete all the nodes |
MATCH (n) DETACH DELETE (n) |
|
Get the node with given id |
MATCH (n) WHERE id(n) = {NODE_ID} RETURN n |
MATCH (n) WHERE id(n) = 4 RETURN n
|
Limit number of items |
MATCH (n) RETURN n LIMIT NUMBER_OF_ITEMS
|
MATCH (n) RETURN n LIMIT 5 |
Match nodes by label and properties |
MATCH (n:label{property1: value1, property2: value2….propertyN: valueN}) RETURN n
|
Get all the nodes with label Location MATCH (n:Location) RETURN n
Get all the nodes which are labelled with Person and whose designation is Manager MATCH (n:Person{designation: “Manager”}) RETURN n |
Match the nodes that has any relationship with other nodes |
MATCH (n1)—(n2) RETURN n1, n2 |
|
Get nodes and their relationships |
MATCH (n1)-[rel]-(n2) RETURN n1, rel, n2
|
|
Specify relationship direction while querying |
MATCH (n1)-[rel]->(n2) RETURN n1, rel, n2 Above statement return the nodes and relationship, if there is any relationship from n1 to n2.
MATCH (n1)<-[rel]-(n2) RETURN n1, rel, n2 Above statement return the nodes and relationship, if there is any relationship from n2 to n1. |
|
Specify relationship type and properties while querying |
MATCH (n1:label{property1: value1, property2: value2….propertyN: valueN})-[rel:relationShipType{property1: value1, property2: value2….propertyN: valueN}]->(n2:label{property1: value1, property2: value2….propertyN: valueN}) RETURN n1, rel, n2 |
MATCH (n1)-[rel:REPORT_TO{from: 1997}]->(n2) RETURN n1, rel, n2 |
Specify multiple relationship types using | |
MATCH (n1)-[rel:RELATIVE_TO | FRIEND_TO]->(n2) RETURN n1, n2, rel
|
Matches the nodes if there is any relationship RELATIVE_TO or FRIEND_TO between nodes from n1 to n2. |
Specify the properties to return |
MATCH (n1)-[rel]->(n2) RETURN n1.propertyName, rel. propertyName, n2.propertyName |
MATCH (n1)-[rel]->(n2) RETURN n1.name, rel.description, n2.name, rel.from
|
Extract specific properties from query result |
MATCH (n1)-[rel]->(n2) RETURN n1.propertyName, rel. propertyName, n2.propertyName
|
Get all the person names MATCH (n:Person) RETURN n.name
Get person names and their relationship details. MATCH (n1)-[rel]->(n2) RETURN n1.name, rel.description, n2.name, rel.from |
Filter data by properties
|
|
Get all the persons who has property age with value 32 MATCH(n:Person{age:32}) RETURN n
Get all the person names who are reporting to Rama Krishna MATCH(n1:Person{name:”Rama Krishna”})<-[:REPORT_TO]->(n2) RETURN n2.name
Get all the persons whose designation is Manager MATCH (n:Person{designation:”Manager”}) Return n
Get all the person name, age who are reporting to CEO directly MATCH (n1)-[:REPORT_TO]->(n2:Person{designation:”CEO”}) Return n1.name, n1.age
|
Filter the data using WHERE clause |
|
Get all the persons who has property age with value 32 MATCH(n:Person) WHERE n.age=32 RETURN n
Get all the person names who are reporting to Rama Krishna MATCH(n1:Person)<-[:REPORT_TO]->(n2) WHERE n1.name=”Rama Krishna” RETURN n2.name
Get all the persons whose designation is Manager MATCH (n:Person) WHERE n.designation=”Manager” Return n
Get all the person name, age who are reporting to CEO directly MATCH (n1)-[:REPORT_TO]->(n2:Person) WHERE n2.designation=”CEO” Return n1.name, n1.age
|
Comparison operators |
|
Get all the persons whose age is equal to 32 MATCH(n:Person) WHERE n.age=32 RETURN n
Get all the persons whose age is not equal to 32. MATCH(n:Person) WHERE n.age<>32 RETURN n
Get all the persons whose age is < 32. MATCH(n:Person) WHERE n.age<32 RETURN n
Get all the persons whose age is > 32 MATCH(n:Person) WHERE n.age>32 RETURN n
Get all the persons whose age is <= 32. MATCH(n:Person) WHERE n.age<=32 RETURN n
Get all the persons whose age is >= 32. MATCH(n:Person) WHERE n.age>=32 RETURN n |
Boolean OR |
|
Get all the persons whose city is Bangalore and designation is Employee MATCH(n:Person) WHERE n.city=”Bangalore” AND n.designation=”Employee” RETURN n |
Boolean AND |
|
Get all the employees whose age is 32 or designation is Manager MATCH(n:Person) WHERE n.age=32 OR n.designation=”Manager” RETURN n |
IN Operator |
|
Get all the persons whose age is either 32 or 45 or 38. MATCH(n:Person) WHERE n.age IN [32, 45, 38] RETURN n |
NOT Operator |
|
Get all the persons whose city is not Bangalore and designation is not Employee MATCH(n:Person) WHERE NOT (n.city=”Bangalore” OR n.designation=”Employee”) RETURN n
Get all the persons whose age is is not 32, 45, 38. MATCH(n:Person) WHERE n.age NOT IN [32, 45, 38] RETURN n |
ORDER BY Caluse |
|
Get the person details by their salary in ascending order. MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary
Get the person details by their salary in descending order MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC
You can even use ORDER BY clause on multiple properties MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC, p.age |
SKIP |
|
Get person details who is earning second highest salary MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.salary DESC SKIP 1 LIMIT 1
Get third oldest person details MATCH (p:Person) RETURN p.name, p.salary, p.age ORDER BY p.age DESC SKIP 2 LIMIT 1 |
AS Clause: To rename properties |
|
MATCH (p:Person) RETURN p.name AS My_Name, p.salary AS My_Salary, p.age AS My_Age ORDER BY p.age |
DISTINCT |
|
Get all the distinct cities where persons are living MATCH (p:Person) RETURN DISTINCT(p.city) AS My_City
Get all the distinct designation available in the organization. MATCH (p:Person) RETURN DISTINCT(p.designation) AS designations |
COUNT |
|
Count number of managers in the rganization MATCH (n:Person{designation:”Manager”}) RETURN COUNT(n)
Count number of employees who are reporting to Manager directly. MATCH (n1:Person{designation:”Manager”})<-[:REPORT_TO]-(n2:Person{designation:”Employee”}) RETURN COUNT(n2)
Count number of employees who are reporting to the director “Kiran” MATCH (n1:Person{designation:”Manager”, name:”Kiran”})<-[:REPORT_TO]-(n2:Person{designation:”Employee”}) RETURN COUNT(n2) |
SUM |
|
Get sum of slaries of all employees MATCH(n:Person) RETURN SUM(n.salary)
Get sum of salaries of employee by their designation MATCH(n:Person) RETURN n.designation, SUM(n.salary) ORDER BY n.designation |
AVG |
|
Get average of slaries of all employees MATCH(n:Person) RETURN AVG(n.salary)
Get average salaries of employee by their designation MATCH(n:Person) RETURN n.designation, AVG(n.salary) ORDER BY n.designation |
MIN |
|
Get Minimum salary of all employees MATCH(n:Person) RETURN MIN(n.salary)
Get Minimum salaries of employee by their designation MATCH(n:Person) RETURN n.designation, MIN(n.salary) ORDER BY n.designation |
MAX |
|
Get Maximum salary of all employees MATCH(n:Person) RETURN MAX(n.salary)
Get Maximum salaries of employee by their designation MATCH(n:Person) RETURN n.designation, MAX(n.salary) ORDER BY n.designation |
Update Operations
Command |
Syntax |
Example or Description |
SET |
Syntax to update properties MATCH …. WHERE …. SET propertyName1 = value, propertyName2 = value2 RETURN ….
Syntax to add new label to a node MATCH …. WHERE …. SET nodeVariable:label1:label2:…labelN RETURN …. |
Update the person name to Kiran Kumar, if his name is Kiran MATCH (p:Person) WHERE p.name=”Kiran” SET p.name=”Kiran Kumar” RETURN p
Add new label ‘EngineeringManager’ to all the managers. MATCH(p:Person{designation:”Manager”}) SET p:EngineeringManager RETURN p
You can even add multiple labels in single query. For example, below statement add labels M1, M2. MATCH(p:Person{designation:”Manager”}) SET p:M1:M2 RETURN p |
REMOVE |
Remove a property from node MATCH …. WHERE …. REMOVE variable.propertyName RETURN ….
Remove a label from node MATCH …. WHERE …. REMOVE variable:labelName RETURN …. |
Remove a property city from the person with name ‘Brahmam’ MATCH (p:Person) WHERE p.name=”Brahmam” REMOVE p.city RETURN p
Remove label Handsome from the person with name ‘Kiran’ and designation ‘Director’. MATCH (p:Person) WHERE p.name=”Kiran” AND p.designation=”Director” REMOVE p:Handsome RETURN p |
Delete Operations
Command |
Syntax |
Example or Description |
Delete all the nodes |
MATCH (n) DETACH DELETE (n) |
|
Delete specific nodes |
MATCH (node:label {properties . . . . . . . . . . }) DETACH DELETE node
(or) MATCH (node:label) WHERE ..... DETACH DELETE node |
Delete all the persons whose designation is Manager MATCH(n:Person{designation:"Manager"}) DETACH DELETE n
Delete all the persons whose designation is Director MATCH(n:Person) WHERE n.designation="Director" DETACH DELETE n |
Previous Next Home
No comments:
Post a Comment