Graph Database
Graph Database
Graph Database
Ø A graph database is a database designed to store and query data
represented in the form of a graph. A graph consists of vertices (also called
nodes) and edges, which represent the relationships between the vertices
Ø Graph database is used for modelling data in the form of graph. Here,
nodes of a graph depict the entities whereas relationships depict the
association of these nodes.
Ø Graph databases are particularly useful for storing and querying data that
has complex relationships, such as social networks, recommendation
2 engines, and fraud detection systems.
CREDITS: Jimmy Lin (University of Maryland)
Popular Graph Database
Ø Neo4j
Ø OrientDB, HypherGraphDB
Ø GraphBase
Ø InfiniteGraph
Ø JanusGraph
Ø Amazon Neptune
Ø ArangoDB
Ø AllegroGraph.
3
CREDITS: Jimmy Lin (University of Maryland)
Graph Database Use Case
Ø Social networks
Graph databases can be used to store and query data about relationships between people, such as
friendships, family relationships, and professional connections. This can be used to build social
networking platforms, recommendation engines, and other applications.
Ø Fraud detection
Graph databases can be used to identify patterns of fraudulent activity by analyzing the relationships
between entities such as individuals, businesses, and transactions.
Ø Recommendation engines
Graph databases can be used to store and query data about users and their interactions with products or
content. This can be used to build recommendation engines that suggest products or content to users
based on their interests and past behavior.
Ø Supply chain management
Graph databases can be used to store and query data about the relationships between different entities in a
supply chain, such as suppliers, manufacturers, and retailers. This can be used to optimize logistics and
supply chain management.
Ø Bioinformatics
Graph databases can be used to store and query data about the relationships between different biological
entities, such as genes, proteins, and diseases. This can be used to study the relationships between
different biological processes and to develop new drugs and treatments.
4
CREDITS: Jimmy Lin (University of Maryland)
Graph Database Properties
Ø Flexible data model
Graph databases use a flexible data model that allows for the representation of complex
relationships between entities. This makes it easy to store and query data that has many different
types of relationships and connections.
Ø Efficient querying
Graph databases are optimized for efficient querying of data, particularly when it comes to
traversing relationships between entities. This makes it easy to find and retrieve data about specific
entities and their relationships with other entities.
Ø Scalability
Graph databases are designed to scale well as the size of the data increases. This makes them
suitable for storing and querying large amounts of data.
Ø ACID transactions
Many graph databases support ACID (Atomicity, Consistency, Isolation, Durability) transactions,
which ensure that data is stored and accessed in a consistent and reliable manner. This is important
for applications that require high levels of data integrity and reliability.
Ø High performance
Graph databases are optimized for high performance and can handle a large number of queries and
updates in real-time. This makes them suitable for use in high-traffic applications.
5
CREDITS: Jimmy Lin (University of Maryland)
Why Graph Databases?
Ø These days, most of the data exist in the form of the relationship between
different objects and more often, relationship between the data is more
valuable than the data itself.
Ø Relational databases store highly structured data which have many records
storing same type of data so that they can be used for storing structured
data and they do not store the relationships between the data.
Ø Unlike other databases, graph databases store relationships and
connections as first-class entities.
Ø Data model for graph databases is easy when compared to other databases
and they can be used with OLTP systems as they provide features like
transactional integrity and operational availability.
6
CREDITS: Jimmy Lin (University of Maryland)
RDBMS Vs Graph Database
1 Tables Graphs
2 Rows Nodes
4 Constraints Relationships
5 Joins Traversal
7
CREDITS: Jimmy Lin (University of Maryland)
Neo4j EcoSystem
8
CREDITS: Jimmy Lin (University of Maryland)
Neo4j
Ø Neo4j is one of the popular Graph Databases and Cypher Query Language (CQL). Neo4j is
Ø Neo4j is the world's leading open source Graph Database which is developed using Java
Ø Neo4j has its own query language that called Cypher Language. It is similar to SQL,
remember one thing Neo4j does not work with tables, row or columns it deals with nodes. It is
more satisfied to see the data in a graph format rather than in a table format.
(X)-[:CreatePattern]->(Y)
9
CREDITS: Jimmy Lin (University of Maryland)
Neo4j
Ø Neo4j deals with nodes and the nodes contains labels that could be “Person”, “Employee”,
Ø Neo4j also have properties like “name”, “employee_id”, “phone_number”, basically that will
Ø Neo4j’s relationship also can contain the properties but this is not mandatory.
Ø Neo4j stores and present the data in the form of graph not in tabular format or not in a Json
format. Here the whole data is represented by nodes and there you can create a relationship
between nodes. That means the whole database collection will look like a graph, that’s why it
is making it unique from other database management system.
Ø MS Access, SQL server all the relational database management system use tables to store or
present the data with the help of column and row but Neo4j doesn’t use tables, row or columns
like old school style to store or present the data.
10
CREDITS: Jimmy Lin (University of Maryland)
Neo4j
Ø Neo4j Graph Database is the core product, a native graph database that is built to store and
Ø by installing on-premises
Ø Neo4j offers a number of commercial licensing options, including free licenses for
development, startups, academic-educational uses and of course, evaluation.
Ø Neo4j Enterprise Edition is available for download as a zipfile/tarball as well as via Debian,
RPM, DockerHub, and more; as well as via under a "bring your own license" model in
11 cloud services, including Google Cloud, Microsoft Azure, and AWS.
CREDITS: Jimmy Lin (University of Maryland)
Neo4j Aura
Ø AuraDB and AuraDS are fully managed cloud service offerings.
Ø Neo4j AuraDB is a graph database as a service. For more information, see the official
documentation.
Ø To interact with an Aura instance, use Neo4j Workspace — a low-code UI-based tool that
helps you to design a data model, preview the model, import the data, and explore and query
the database.
Ø Neo4j AuraDS makes it easy to run graph algorithms on Neo4j by integrating two main
components:
Ø Neo4j Database, where graph data are loaded and stored, and Cypher® queries and all
database operations (for example user management, query termination, etc.) are executed.
Ø Graph Data Science, a software component installed in the Neo4j Database, which
12
CREDITS: Jimmy Lin (University of Maryland)
Neo4j Tools
Ø Neo4j provides a variety of tools that aim to make learning and development of graph
applications easier.
Ø Neo4j Desktop — a local development environment for working with Neo4j, whether using
local database instances or databases located on remote servers. Free download includes Neo4j
Enterprise Edition license.
Ø Neo4j Browser — online browser interface to query and view the data in the database. Basic
Ø Neo4j Operations Manager (NOM) — a UI-based tool that enables a database administrator
to monitor, administer, and operate all of the Neo4j database management systems in an
Enterprise Edition.
Ø Data Importer — a no-code tool that allows you to load data from flat files (.csv and .tsv),
define a graph model, map your data to it, and import it to your Neo4j database.
Ø Neo4j Bloom — visualization tool for business users that does not require any code or
Ø easy to learn
14
CREDITS: Jimmy Lin (University of Maryland)
Connecting to Neo4j
Ø Neo4j provides a wide range of opportunities for integrating Neo4j with
any working environment and creating applications.
15
CREDITS: Jimmy Lin (University of Maryland)
Advantages of Neo4j
Ø Flexible data model − Neo4j offers a flexible, simple but yet powerful data model, which can
Ø High availability − Neo4j is highly available for large enterprise real-time applications with
transactional guarantees.
Ø Connected and semi structures data − Using Neo4j, it is easy to represent connected and
semi-structured data.
Ø Easy retrieval − Using Neo4j, it is not only easy to represent but it is also easily to retrieve
Ø Cypher query language − Neo4j offers a declarative query language for representing the
graph visually, using an ascii-art syntax. Commands of this language are in human readable
format and very easy to learn.
Ø No joins − Using Neo4j, it does NOT require complex joins to retrieve connected/related data
as it is very easy to retrieve its adjacent node or relationship details without joins or indexes.
16
CREDITS: Jimmy Lin (University of Maryland)
Features of Neo4j
Ø Data model (flexible schema) − Neo4j follows a data model named native property graph
model. Here, the graph consists of nodes (entities) and these nodes will be connected with
each other (depicted by relationships). Nodes and relationships store data in key-value pairs
called as properties.
In Neo4j, you don’t have to follow a fixed schema. You can add or remove properties
as based on your requirement. It also provides schema constraints.
Ø ACID properties − Neo4j supports full ACID (Atomicity, Consistency, Isolation, and
Durability) rules.
Ø Scalability and reliability – Database can be scaled by increasing the number of reads/writes,
and the volume without effecting the query processing speed and data integrity. Neo4j also
provides support for replication for data safety and reliability.
17
CREDITS: Jimmy Lin (University of Maryland)
Features of Neo4j
Ø Cypher Query Language − Neo4j offers a powerful declarative query language known as
Cypher which uses ASCII-art for depicting graphs. Cypher is easy to learn and is used for
creating and retrieving relations between data without using the complex queries like Joins.
Ø Built-in web application − Neo4j offers a built-in Neo4j Browser web application. Using this,
Ø REST API to work with programming languages such as Java, Spring, Scala etc.
Ø It supports two kinds of Java API: Cypher API and Native Java API to develop Java
applications. Apart from this, you can also work with other databases like MongoDB,
Cassandra, etc.
18
CREDITS: Jimmy Lin (University of Maryland)
Where and how is Neo4j used?
Ø Neo4j is used today by thousands of startups, educational institutions, and large
enterprises in all sectors including financial services, government, energy,
technology, retail, and manufacturing.
Ø From innovative new technology to driving businesses, users are generating
insights with graph, generating new revenue, and improving their overall efficiency.
19
CREDITS: Jimmy Lin (University of Maryland)
Neo4j Property Graph Data Model
Ø Neo4j Graph Database follows the Property Graph Model for storing and managing its data.
Ø Nodes will be represented using circle and Relationships will be represented using arrow
keys
Ø Each Relationship contains "Start Node" or "From Node" and "To Node" or "End Node"
Ø In Property Graph Data Model, relationships should be directional. If you try to create
Ø Your data is stored without restricting it to a pre-defined model, allowing a very flexible way
Ø Neo4j Graph Database will store all of its data in Nodes and Relationships. There is no need of
any additional RRBMS Database nor any SQL database for storing Neo4j database data. It
stores its data in terms of Graphs in its native format.
21
CREDITS: Jimmy Lin (University of Maryland)
Neo4j Property Graph Data Model
Ø Main building blocks of Graph DB Data Model are
Ø Nodes
Ø Relationships
Ø Properties
Ø Here, Nodes are represented using Circles and relationships are represented using Arrows.
Relationships are directional. Node's data can be represented in terms of Properties (key-value
pairs).In Neo4j, information is organized as nodes, relationships, and properties.
22
CREDITS: Jimmy Lin (University of Maryland)
Graph Data Model
Ø Nodes are the entities in the graph.
Ø Nodes can be tagged with labels, representing their different roles in your domain (for
example, Person).
Ø Nodes can hold any number of key-value pairs, or properties (for example, name).
Ø Node labels may also attach metadata (such as index or constraint information) to certain
nodes.
Ø Relationships provide directed, named connections between two node entities (for example,
Ø Relationships always have a direction, a type, a start node, and an end node, and they can have
Ø Nodes can have any number or type of relationships without sacrificing performance.
Ø Although relationships are always directed, they can be navigated efficiently in any direction.
23
CREDITS: Jimmy Lin (University of Maryland)
Building Blocks in Neo4j
Ø Neo4j Graph Database has below building blocks
Ø Nodes
Ø Properties
Ø Relationships
Ø Labels
Ø Data Browser
Node
24 Here, Node Name = "Employee" and it contains a set of properties as key-value pairs.
CREDITS: Jimmy Lin (University of Maryland)
Building Blocks in Neo4j
Properties
Ø Property is a key-value pair for describing Graph Nodes and Relationships.
Key = Value
Where Key is a String and Value may be represented using any Neo4j Data types.
Ø Relationships
Relationships are another major building block of a Graph Database. It connects two nodes
as depicted in below figure
Here, Emp and Dept are two different nodes. "WORKS_FOR" is a relationship between Emp and
Dept nodes.
Ø As it denotes, the arrow mark from Emp to Dept, this relationship describes
Ø Emp WORKS_FOR Dept
Ø Each relationship consists of one start node and one end node.
Ø Here, "Emp" is a start node, and "Dept" is an end node.
Ø As this relationship arrow mark represents a relationship from "Emp" node to "Dept" node,
this relationship is known as an "Incoming Relationship" to "Dept" Node and "Outgoing
Relationship" to "Emp" node.
25
CREDITS: Jimmy Lin (University of Maryland)
Building Blocks in Neo4j
Similar to nodes, relationships also consist of properties as key-value pairs.
Labels
consists of one or more labels. New labels can be created for existing nodes or relationships
and existing labels can be removed from the existing nodes or relationships.
Ø Left side node has a Label: "Emp" and the right-side node has a Label: "Dept".
Neo4j CQL
Ø Neo4j is a query language used for Neo4j Graph Database.
Ø Neo4j is a declarative pattern-matching language.
Ø It Follows SQL like syntax.
Ø Syntax is very simple and it is in human readable format.
27
CREDITS: Jimmy Lin (University of Maryland)
Features of Neo4j
Ø Neo4j CQL supports many clauses like WHERE, ORDER BY, etc., for
writing very complex queries in an easy manner.
Ø Neo4j CQL supports few functions like String, Aggregation. Apart from
these, it also supports some Relationship Functions.
28
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Clauses
Ø Writing clauses
Ø Reading clauses
Ø Projecting clauses
Ø Reading sub-clauses
Ø Reading hints
Ø Reading/Writing clauses
Ø Set Operations
Ø Importing Data
Ø Schema clauses
29
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Read Clauses
S.No Read Clauses Usage
30
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Write Clauses
S.No Write Clauses Usage
1 CREATE Used for creating nodes, relationships, and properties.
It verifies if the specified pattern exists in the graph. If
2 MERGE
not, it creates the pattern.
Used for updating labels on nodes, properties on nodes
3 SET
and relationships.
Used for deleting nodes and relationships or paths etc.
4 DELETE
from the graph.
Used for removing properties and elements from
5 REMOVE
nodes and relationships.
6 FOREACH This class is used for updating the data within a list.
CREATE and MATCH can be used for getting a
7 CREATE UNIQUE unique pattern by matching the existing pattern and
creating the missing one.
Import CSV files
8 Load CSV is used for importing data from .csv files.
with Cypher
31
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL General Clauses
S.No General Clauses Usage
1 RETURN Used for defining what to include in the query result set.
Used for arranging the output of a query in order. It is used
2 ORDER BY
along with the clauses RETURN or WITH.
3 LIMIT Used for limiting the rows in the result to a specific value.
32
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Functions
S.No CQL Functions Usage
1 String Strings are used to work with String literals.
Aggregation used for performing some aggregation
2 Aggregation
operations on CQL Query results.
A relationship is used to get details of relationships such as
3 Relationship
startnode, endnode, etc.
33
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Datatypes
S.No CQL Data Type Usage
Boolean is used for representing Boolean literals: true,
1 boolean
false.
1 Mathematical +, -, *, /, %, ^
4 String +
6 Regular Expression = -
35
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Boolean Operators
S.No CQL Boolean Usage
36
CREDITS: Jimmy Lin (University of Maryland)
Neo4j CQL Comparison Operators
S.No CQL Comparison Usage
>=
6 It is a Neo4j CQL "Greater Than Or Equal To" operator.
37
CREDITS: Jimmy Lin (University of Maryland)
Neo4J CQL Creating Nodes
Verification
MATCH(n) Return n
Verification
MATCH(n1,n2,n3) Return n1,n2,n3
Create Node with Label and Multiple Labels
3. Creating a Node with a Label
A label in Neo4j is used for grouping (classify) the nodes using labels. Label can be
created for a node in Neo4j using CREATE clause.
Syntax
CREATE (node:label);
Note: − Semicolon (;) is optional.
Example
CREATE (Dhawan:player)
Verification
MATCH(Dhawan) Return Dhawan
Verification
MATCH(Dhawan) Return Dhawan
Create Node with Properties
5. Creating a Node with Properties
Properties are the key-value pairs using which a node stores data. You can create a node
with properties using the CREATE clause. You need to specify these properties separated by commas
within the flower braces “{ }”.
Syntax
CREATE (node:label { key1: value, key2: value, . . . . . . . . . })
Example
CREATE (Dhawan:player{name: "Shikar Dhawan", YOB: 1985, POB: "Delhi"})
Verification
MATCH(Dhawan) Return Dhawan
Syntax
CREATE (node1)-[:RelationshipType]->(node2)
Example
First, create two nodes Ind and Dhawan in the database, as shown below.
CREATE (Dhawan)-[r:BATSMAN_OF]->(Ind)
At last, return both the nodes to see the created relationship.
Syntax :
MATCH (a:LabeofNode1), (b:LabeofNode2)
WHERE a.name = "nameofnode1" AND b.name = " nameofnode2"
CREATE (a)-[: Relation]->(b)
RETURN a,b
Example
MATCH (a:player), (b:Country) WHERE a.name = "Shikar Dhawan" AND b.name = "India"
Example:
Before proceeding with the example, first create a node named Dhawan as
shown below.
Below is a sample Cypher Query which removes the property named POB from
this node using the SET clause as shown below.
Example:
Below is a sample Cypher Query which creates multiple properties in a node
using the SET clause in Neo4j.
To delete a particular node, mention all the details of the node in the place of “n”
in the above query.
Syntax:
Below is the syntax for deleting a particular node from Neo4j using the DELETE clause.
MATCH (node:label {properties . . . . . . . . . . })
DETACH DELETE node
Before proceeding with the example, create a node “Ishant” in the Neo4j database as shown
below.
CREATE (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
Below is a sample Cypher Query which deletes the above created node using the DELETE clause.
MATCH (Ishant:player {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
DETACH DELETE Ishant
Neo4J CQL - REMOVE CLAUSE
How remove clause works in Neo4j?
REMOVE clause is used for removing the properties and labels from graph
elements (Nodes or Relationships).
Main difference between Neo4j CQL DELETE and REMOVE commands is
Ø DELETE operation is used for deleting nodes and associated relationships.
Ø REMOVE operation is used for removing labels and properties.
Removing a Property
A property of a node can be removed using MATCH along with the REMOVE clause.
Syntax:
MATCH (node:label{properties . . . . . . . })
REMOVE node.property
RETURN node
Example:
Before proceeding with the example, create a node named Dhoni as shown below.
CREATE (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
Below is a sample Cypher Query to remove the above created node using the REMOVE clause.
MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
REMOVE Dhoni.POB
RETURN Dhoni
Neo4J CQL - REMOVE CLAUSE
Removing a Label from a Node
Same like property, a label can be removed from an existing node using remove clause.
Syntax:
MATCH (node:label {properties . . . . . . . . . . . })
REMOVE node:label
RETURN node
Example:
Below is a sample Cypher Query for removing a label from an existing node using the
remove clause.
MATCH (Dhoni:player {name: "MahendraSingh Dhoni", YOB: 1981, POB: "Ranchi"})
REMOVE Dhoni:player
RETURN Dhoni
Neo4J CQL - REMOVE CLAUSE
Removing Multiple Labels
Multiple labels can also be removed from an existing node.
Syntax:
MATCH (node:label1:label2 {properties . . . . . . . . })
REMOVE node:label1:label2
RETURN node
Example:
Before proceeding with the example, create a node Ishant as shown below.
CREATE (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
Below is a sample Cypher Query for removing multiple labels from a node.
MATCH (Ishant:player:person {name: "Ishant Sharma", YOB: 1988, POB: "Delhi"})
REMOVE Ishant:player:person
RETURN Ishant
Neo4J CQL - FOREACH CLAUSE
What is the use of Foreach clause in Neo4j?
FOREACH clause is used for updating data within a list whether components of a path, or result
of aggregation.
Syntax
Below is the syntax of the FOREACH clause.
MATCH p = (start node)-[*]->(end node)
WHERE start.node = "node_name" AND end.node = "node_name"
FOREACH (n IN nodes(p)| SET n.marked = TRUE)
Example:
Before proceeding with the example, create a path p in Neo4j database as shown below.
CREATE p = (Dhawan {name:"Shikar Dhawan"})-[:TOPSCORRER_OF]->(Ind{name:
"India"})-[:WINNER_OF]->(CT2013{name: "Champions Trophy 2013"})
RETURN p
Below is a sample Cypher Query which adds a property to all the nodes along the path using
the FOREACH clause.
MATCH p = (Dhawan)-[*]->(CT2013)
WHERE Dhawan.name = "Shikar Dhawan" AND CT2013.name = "Champions Trophy 2013"
FOREACH (n IN nodes(p)| SET n.marked = TRUE)
Verification
Match(Dhawan) Return Dhawan
Neo4J CQL - MATCH CLAUSE
1. Get All Nodes Using Match
MATCH clause of Neo4j can be used to retrieve all nodes in the Neo4j database.
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)
CREATE (Dhawan:player{name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
CREATE (Jadeja:player {name: "Ravindra Jadeja", YOB: 1988, POB:
"NavagamGhed"})
CREATE (Dhawan)-[:TOP_SCORER_OF {Runs:363}]->(Ind)
CREATE (Jadeja)-[:HIGHEST_WICKET_TAKER_OF {Wickets:12}]->(Ind)
Below is the query which returns all the nodes in Neo4j database.
MATCH (n) RETURN n
Neo4J CQL - MATCH CLAUSE
2.Getting All Nodes Under a Specific Label
Match clause can be used to get all the nodes under a specific label.
Syntax:
Below is the syntax to get all the nodes under a specific label.
MATCH (node:label)
RETURN node
Below is a sample Cypher Query, which returns all the nodes in the database under the
label player.
MATCH (n:player)
RETURN n
3.Match by Relationship
Nodes can be retrieved based on relationship using the MATCH clause.
Syntax:
Below is the syntax for retrieving nodes based on the relationship using the MATCH clause.
MATCH (node:label)<-[: Relationship]-(n)
RETURN n
Below is a sample Cypher Query to retrieve nodes based on relationship using the MATCH
clause.
MATCH (Ind:Country {name: "India", result: "Winners"})<-[: TOP_SCORER_OF]-(n)
RETURN n.name
3.Delete All Nodes: All the nodes can be deleted using MATCH clause.
MATCH (n) detach delete n
Neo4J CQL - OPTIONAL MATCH CLAUSE
What is optional match clause in Neo4j?
OPTIONAL MATCH clause is used for searching the pattern described in it and nulls are used
for missing parts of the pattern.
OPTIONAL MATCH is similar to the match clause, only difference is that it returns null as a
result of the missing parts of the pattern.
Syntax:
Below is the syntax of the OPTIONAL MATCH with relationship.
MATCH (node:label {properties. . . . . . . . . . . . . .})
OPTIONAL MATCH (node)-->(x)
RETURN x
Below is a sample Cypher Query which tries to retrieve the relations from the node ICCT2013.
As there are no such nodes, it will return null.
MATCH (a:Tornament {name: "ICC Champions Trophy 2013"})
OPTIONAL MATCH (a)-->(x)
RETURN x
Neo4J CQL - WHERE CLAUSE
What is the use of where clause in Neo4j?
Like SQL, Neo4j CQL offers WHERE clause in CQL MATCH command for filtering the
results of a MATCH Query.
Syntax:
Below is the syntax of the WHERE clause.
MATCH (label)
WHERE label.country = "property"
RETURN label
Before proceeding with the example, create five nodes in the database as shown below.
CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"}
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"}
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
CREATE(Ind:Country {name: "India", result: "Winners"})
Below is a sample Cypher Query which will return all the players (nodes) which belong to the country India
using WHERE clause.
MATCH (player)
WHERE player.country = "India"
RETURN player
Neo4J CQL - WHERE CLAUSE
WHERE Clause with Multiple Conditions
WHERE clause can also be used to verify multiple conditions.
Syntax:
Below is the syntax to use WHERE clause in Neo4j with multiple conditions.
MATCH (emp:Employee)
WHERE emp.name = 'Abc' AND emp.name = 'Xyz'
RETURN emp
Below is a sample Cypher Query which filters the nodes in the Neo4j database using two conditions.
MATCH (player)
WHERE player.country = "India" AND player.runs >=175
RETURN player
Neo4J CQL - WHERE CLAUSE
Using Relationship with Where Clause
Where clause can also be used to filter the nodes using the relationships.
Example
Below is a sample Cypher Query for
retrieving the top scorer of India
using WHERE clause as shown below.
MATCH (n)
WHERE (n)-[: TOP_SCORER_OF]->
( {name: "India", result: "Winners"})
RETURN n
Neo4J CQL - COUNT Function
How to use count function in Neo4j?
Assume that we have created a graph in the database with below details.
Count
count() function is used for counting the number of rows.
Syntax:
Below is the syntax of the count function.
MATCH (n { name: 'A' })-->(x)
RETURN n, count(*)
Example
Below is a sample Cypher Query which demonstrates
the usage of the count() function.
Match(n{name: "India", result: "Winners"})--(x)
RETURN n, count(*)
Group Count :
COUNT clause is also used for counting the groups of relationship types.
Below is a sample Cypher Query which counts and returns the number of nodes participating in each relation.
Match(n{name: "India", result: "Winners"})-[r]-(x)
RETURN type (r), count(*)
Neo4J CQL - RETURN Clause
What is the use of Return clause in Neo4j?
RETURN clause is used to return nodes, relationships, and properties in Neo4j. In this chapter,
we will explain how to
Ø Return nodes
Ø Return multiple nodes
Ø Return relationships
Ø Return properties
Ø Return all elements
Ø Return a variable with column alias
1. Returning Nodes
You can return a node using the RETURN clause.
Syntax:
Below is a syntax to return nodes using the RETURN clause.
Create (node:label {properties})
RETURN node
Example
Below 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
Neo4J CQL - RETURN Clause
2. Returning Multiple Nodes
Multiple nodes can be returned using the return clause.
Syntax:
Below is a syntax to return nodes using the RETURN clause.
Create (node:label {properties}),(node1:label {properties})
RETURN node, node1
Example
Below is a sample Cypher Query to return multiple nodes using the return clause.
Create CREATE (Ind:Country {name: "India", result: "Winners"})
CREATE (CT2013:Tornament {name: "ICC Champions Trophy 2013"})
RETURN Ind, CT2013
Neo4J CQL - RETURN Clause
3. Returning Relationships
You can also return relationships using the Return clause.
Syntax:
Below is the syntax to return relationships using the RETURN clause.
CREATE (node1)-[Relationship:Relationship_type]->(node2)
RETURN Relationship
Example
Below 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
Neo4J CQL - RETURN Clause
4. Returning Properties
You can also return properties using the Return clause.
Syntax:
Below is the syntax to return properties using the RETURN clause.
Match (node:label {properties . . . . . . . . . . })
Return node.property
Example
Below 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
5. Returning All Elements
You can return all the elements in the Neo4j database using the RETURN clause.
Below is an example Cypher Query to return all the elements in the database.
Match p = (n {name: "India", result: "Winners"})-[r]-(x)
RETURN *
Neo4J CQL - RETURN Clause
5. Returning a Variable with a Column Alias
You can return a particular column with alias using RETURN clause in Neo4j.
Example
Below 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
Neo4J CQL - ORDER BY Clause
What is the use of order by clause in Neo4j?
Result data can be arranged in order using the ORDER BY clause.
Syntax
MATCH (n)
RETURN n.property1, n.property2 . . . . . . . .
ORDER BY n.property
Example
Before proceeding with the example, create 5 nodes in Neo4j database as shown below.
CREATE(Dhawan:player{name:"shikar Dhawan", YOB: 1985, runs:363, country: "India"})
CREATE(Jonathan:player{name:"Jonathan Trott", YOB:1981, runs:229, country:"South Africa"})
CREATE(Sangakkara:player{name:"Kumar Sangakkara", YOB:1977, runs:222, country:"Srilanka"})
CREATE(Rohit:player{name:"Rohit Sharma", YOB: 1987, runs:177, country:"India"})
CREATE(Virat:player{name:"Virat Kohli", YOB: 1988, runs:176, country:"India"})
Below is a sample Cypher Query which returns the above created nodes in the order of the runs scored by the
player using the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs
Neo4J CQL - ORDER BY Clause
Ordering Nodes by Multiple Properties
Nodes can be arranged based on multiple properties using ORDEYBY clause.
Syntax
MATCH (n)
RETURN n
ORDER BY n.properties1, n.properties
Example
Below is a sample Cypher Query which arranges the nodes created earlier in this chapter based on
the properties - runs and country.
MATCH (n)
RETURN n.name, n.runs, n.country
ORDER BY n.runs, n.country
Neo4J CQL - ORDER BY Clause
Ordering Nodes by Descending Order
Nodes can be arranged in a database in a descending order using the ORDERBY clause.
Syntax
MATCH (n)
RETURN n
ORDER BY n.name DESC
Example
Below is a sample Cypher Query which arranges the nodes in a database in a descending order
using the ORDERBY clause.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
Neo4J CQL - LIMIT Clause
How to use limit clause neo4j?
limit clause is used for limiting the number of rows in the output.
Syntax
MATCH (n)
RETURN n
ORDER BY n.name
LIMIT 3
Example
Below is a sample Cypher Query which returns the nodes created above in a descending order and
limits the records in the result to 3.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
LIMIT 3
Limit with expression
LIMIT clause can also be used with expression.
Example
Below is a sample Cypher Query which limits the records using an expression.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
LIMIT toInt(3 * rand())+ 1
Neo4J CQL - SKIP Clause
What is the use of skip clause in Neo4j?
SKIP clause is used for defining from which row to start including the rows in the output.
MATCH (n)
RETURN n
ORDER BY n.name
SKIP 3
Below is a sample Cypher Query which returns all the nodes in the database skipping the first 3
nodes.
MATCH (n)
RETURN n.name, n.runs
ORDER BY n.runs DESC
SKIP 3
Example
Below is a sample Cypher Query which unwinds a list.
UNWIND [a, b, c, d] AS x
RETURN x
Neo4J CQL - STRING Functions
What are string functions in Neo4j?
Similar to SQL, Neo4J CQL also provides a set of String functions which are used in CQL
Queries for getting the required results.
String Fucntions List
Ø UPPER - > This is used for changing all letters into upper case letters.
Ø LOWER - > This is used for changing all letters into lower case letters.
Ø SUBSTRING - > This is used for getting substring of a given String.
Ø REPLACE -> This is used for replacing a substring with a given substring of a
String.
Neo4J CQL - STRING Functions
What is upper function in Neo4j?
Upper function takes a string as an input and converts it into upper case letters. All CQL
functions should use "( )" brackets. UPPER (<input-string>)
Example: Create 5 nodes in Neo4j
Below is a sample cypher query which demonstrates the usage of the function UPPER() in
Neo4j. Here we are converting names of all the players into upper case.
MATCH (n:player)
RETURN UPPER(n.name), n.YOB, n.POB
Below is a sample Cypher Query to create an index on the node Dhawan in Neo4j.
CREATE INDEX IND1 FOR(Dhawan:player) ON(Dhawan.name)
2. Deleting an Index
Neo4j CQL provides a "DROP INDEX" command which is used to drop an existing
index of a Node or Relationships property.
Syntax: DROP INDEX INDEX_NAME FOR(Node:label) ON(node.property)
Example:
Below is a sample Cypher Query to create an index on the node named “Dhawan” in Neo4j
DROP INDEX IND1 FOR(Dhawan:player) ON(Dhawan.name)
Neo4J CQL - CREATE UNIQUE CONSTRAINT
How to create a unique constraint in Neo4j?
In Neo4j database, CQL CREATE command will always create a new node or
relationship which means even though you use the same values, it will insert a new row.
Based on the application requirements for some nodes or relationships, this duplication
should be avoided. For this, some database constraints should be used to create a rule on
one or more properties of a node or relationship.
Like SQL, Neo4j database will also support UNIQUE constraint on node or
relationship properties. UNIQUE constraint is used to avoid duplicate records and to
apply data integrity rule.
Neo4J CQL - CREATE UNIQUE CONSTRAINT
Create UNIQUE Constraint
Neo4j CQL offers "CREATE CONSTRAINT" command for creating unique
constraints on node or relationship properties.
Syntax:
MATCH (root {name: "Dhawan"})
CREATE UNIQUE (root)-[:LOVES]-(someone)
RETURN someone
Example:
Before proceeding with the example, create 5 nodes as shown below.
CREATE(Dhawan:player{id:001, name: "shikar Dhawan", YOB: 1995, POB: "Delhi"})
CREATE(Jonathan:player {id:002, name: "Jonathan Trott", YOB: 1981, POB: "CapeTown"})
CREATE(Sangakkara:player {id:003, name: "Kumar Sangakkara", YOB: 1977, POB: "Matale"})
CREATE(Rohit:player {id:004, name: "Rohit Sharma", YOB: 1987, POB: "Nagpur"})
CREATE(Virat:player {id:005, name: "Virat Kohli", YOB: 1988, POB: "Delhi"})
Below is a sample Cypher Query for creating a UNIQUE constraint on the property id using Neo4j.
CREATE (Jadeja:player {id:002, name: "Ravindra Jadeja", YOB: 1988, POB:
"NavagamGhed"})
Verification: Now, try adding another node with a redundant id value. Here, we are
trying to create a node with id 002. Same above query
Neo4J CQL - CREATE UNIQUE CONSTRAINT
What is the use of drop unique constraint in Neo4j?
Neo4j CQL offers "DROP CONSTRAINT" command for deleting existing Unique
constraint from a node or relationship property.
Syntax:
DROP CONSTRAINT ON (node:label)
ASSERT node.id IS UNIQUE
Example:
Below is a sample Cypher Query to remove the UNIQUE constraint on the property id.
DROP CONSTRAINT ON (n:player)
ASSERT n.id IS UNIQUE