neo4j
  1. neo4j-what-is-neo4jcql

What is Neo4j CQL

Neo4j is a popular graph database management system that allows you to model, store, retrieve and manipulate data in a graph format. Cypher Query Language (CQL) is the query language used by Neo4j to interact with the database and perform various operations like creating nodes and relationships, updating nodes and relationships, and querying data.

Syntax

The syntax of Neo4j CQL is as follows:

MATCH (node1)-[rel]->(node2)
WHERE <condition>
RETURN <expression>

In the above syntax:

  • MATCH is used to specify the pattern of the nodes and relationships to retrieve.
  • (node1) and (node2) represent nodes in the graph.
  • [rel] represents a relationship between two nodes.
  • WHERE is used to filter the results based on certain conditions.
  • <condition> specifies the condition that needs to be satisfied for the nodes and relationships to be returned.
  • <expression> represents the properties of the nodes and relationships to return.

Example

Let's consider a sample Neo4j graph database with the following nodes and relationships:

Nodes:

(Author1) - {name: "John Doe", age: 35}
(Author2) - {name: "Jane Smith", age: 40}
(Book1) - {title: "The Great Gatsby", year: 1925}
(Book2) - {title: "To Kill a Mockingbird", year: 1960}

Relationships:

(Author1) - [:WROTE] -> (Book1)
(Author2) - [:WROTE] -> (Book2)

To retrieve all the books in the graph, we can use the following CQL query:

MATCH (b:Book)
RETURN b

This will return the following output:

(b1) - {title: "The Great Gatsby", year: 1925}
(b2) - {title: "To Kill a Mockingbird", year: 1960}

Explanation

In the above example, we have used the MATCH keyword to retrieve all nodes labeled as Book. For each Book node retrieved, the node and its properties are returned, as specified by the RETURN keyword.

Use

Neo4j CQL is used to interact with the graph database and perform various operations like creating nodes and relationships, updating nodes and relationships, and querying data. It is a powerful language that allows you to express complex graph queries with ease.

Important Points

  • Neo4j CQL is a declarative language much like SQL.
  • It uses patterns to match nodes and relationships in the graph.
  • Neo4j CQL can be used to perform a wide range of graph operations.
  • It supports advanced features like filtering, sorting, aggregating, and grouping data.

Summary

Neo4j CQL is the query language used by Neo4j to interact with the database and perform various operations like creating nodes and relationships, updating nodes and relationships, and querying data. It is a declarative language much like SQL, and uses patterns to match nodes and relationships in the graph. Neo4j CQL is a powerful language that allows you to express complex graph queries with ease and supports advanced features like filtering, sorting, aggregating, and grouping data.

Published on: