Neo4j Query Cypher Language


Neo4j database is one of the most popular choices in graph databases. One of the reasons for this is its powerful query language, Cypher. Neo4j has developed its own query language called Cypher, which differs from SQL in that it operates on nodes rather than tables, rows, and columns. Its syntax is similar to SQL. But it is better suited for working with graph data. This is because Neo4j is a graph database that stores data in nodes and relationships. It can be visualized in a graph format rather than the traditional table format.

In this article, we will discuss Cypher, its importance and various examples.

Introduction

Cypher is a declarative query language. It allows you to query graph databases, like Neo4j. It has become the go-to language for working with Neo4j. Cypher is easy to learn and use. Even for people who have no previous experience with graph databases.

Cypher is important for several reasons. It allows complex graph queries in a simple and intuitive way. Cypher is designed to be extensible. Cypher is highly optimized for performance.

Syntax

Using Cypher to query a Neo4j database is relatively straightforward. The basic syntax for a Cypher query is as follows −

MATCH (n)
WHERE ...
RETURN ...
  • `MATCH` keyword is used to specify the pattern of nodes and relationships that match in the graph.

  • `WHERE` clause is used to filter the results based on specific criteria.

  • `RETURN` keyword is used to specify the data that returns from the query.

Example-1

For example, the following Cypher query would find all nodes in the graph that have the label "Person" and return their names and ages:

MATCH (p:Person)
RETURN p.name, p.age

Cypher also supports a wide range of functions and operators. These allow us to manipulate and analyze graph data in a variety of ways. Some common examples include −

  • `COUNT` returns the number of nodes or relationships that match a particular pattern

  • `ORDER BY` sorts the results of a query based on a specified property or expression

  • `DISTINCT` returns only unique results

  • `LIMIT` limits the number of results returned by a query

Example-2

To illustrate the differences between Neo4j's Cypher and SQL, consider the following example. The Cypher statement below retrieves the "Company" node whose "name" property is set to "TutorialsPoint", using the variable "G" to hold the returned data.

MATCH (G:Company { name:"TutorialsPoint" })
RETURN G

In contrast, the equivalent SQL query would be

SELECT * FROM Company WHERE name = "TutorialsPoint";

Neo4j was designed as a NoSQL database. It can also be used effectively with relational databases. But, Neo4j's query language does not use SQL. It relies on the Cypher language to interact with its graph-based data model.

ASCII-Art Syntax:

Neo4j uses an ASCII-Art syntax to represent patterns in its graph data model. Nodes are represented by parentheses "( )", while relationships between nodes are represented by "->". To specify the type of relationship, brackets "[]" are used, followed by a label. For example, the pattern "(X)-[:TutorialsPoint]->(Y)" represents a relationship of type "TutorialsPoint" between two nodes, X and Y.

Defining Data

  • Nodes in Neo4j are labeled, which helps define the type of value they represent. Labels can be anything from "Person" to "Employee" or "Employer".

  • Nodes can also have properties like "name", "employee_id", and "phone_number". These provide additional information.

  • Relationships in Neo4j can also have properties, but it's not necessary.

  • Relationships between nodes are defined by the nature of their connection, such as "X works for TutorialsPoint". This relationship can be expressed in Cypher language as (X)-[:WORK]->(TutorialsPoint).

  • In the example query "MATCH (G:Company { name:"TutorialsPoint" }) RETURN G", "Company" is the node's label and "name" is one of its properties. The query retrieves the node labeled "Company" with the name "TutorialsPoint".

Conclusion

Cypher allows querying and analyzing graph data in a Neo4j database. Cypher is an essential tool for working with graph databases.

We have discussed the Cypher query language used in Neo4j, a popular graph database. Cypher is a declarative language that operates on nodes and relationships. It allows complex queries in an intuitive way. The syntax includes keywords such as MATCH, WHERE, and RETURN. Cypher supports a variety of functions and operators, such as COUNT and ORDER BY, and can be used to manipulate and analyze graph data. Neo4j's ASCII-Art syntax represents patterns in its graph data model. Nodes are labeled, and relationships can have properties. Cypher is optimized for performance and allows for extensibility. Overall, Cypher is an essential tool for querying and analyzing graph data in a Neo4j database.

Updated on: 18-May-2023

662 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements