Neo4j CQL – Introduction

  • Post author:
  • Post category:Neo4j
  • Post comments:0 Comments

CQL stands for Cypher Query Language. Like Oracle Database has query language SQL, Neo4j has CQL as query language.

Neo4j CQL

  • Is a query language for Neo4j Graph Database.
  • Is a declarative pattern-matching language.
  • Follows SQL like syntax.
  • Syntax is very simple and in human readable format.

Like Oracle SQL

  • Neo4j CQL has commands to perform Database operations.
  • Neo4j CQL supports many clauses such as WHERE, ORDER BY, etc., to write very complex queries in an easy manner.
  • Neo4j CQL supports some functions such as String, Aggregation. In addition to them, it also supports some Relationship Functions.

Neo4j CQL Clauses

Following are the read clauses of Neo4j Cypher Query Language −

Sr.NoRead ClausesUsage
1MATCHThis clause is used to search the data with a specified pattern.
2OPTIONAL MATCHThis is the same as a match, the only difference being it can use nulls in case of missing parts of the pattern.
3WHEREThis clause id is used to add contents to the CQL queries.
4STARTThis clause is used to find the starting points through the legacy indexes.
5LOAD CSVThis clause is used to import data from CSV files.

Following are the write clauses of Neo4j Cypher Query Language −

Sr.NoWrite ClauseUsage
1CREATEThis clause is used to create nodes, relationships, and properties.
2MERGEThis clause verifies whether the specified pattern exists in the graph. If not, it creates the pattern.
3SETThis clause is used to update labels on nodes, properties on nodes, and relationships.
4DELETEThis clause is used to delete nodes and relationships or paths etc. from the graph.
5REMOVEThis clause is used to remove properties and elements from nodes and relationships.
6FOREACHThis class is used to update the data within a list.
7CREATE UNIQUEUsing the clauses CREATE and MATCH, you can get a unique pattern by matching the existing pattern and creating the missing one.
8Importing CSV files with CypherUsing Load CSV you can import data from .csv files.

Following are the general clauses of Neo4j Cypher Query Language −

Sr.NoGeneral ClausesUsage
1RETURNThis clause is used to define what to include in the query result set.
2ORDER BYThis clause is used to arrange the output of a query in order. It is used along with the clauses RETURN or WITH.
3LIMITThis clause is used to limit the rows in the result to a specific value.
4SKIPThis clause is used to define from which row to start including the rows in the output.
5WITHThis clause is used to chain the query parts together.
6UNWINDThis clause is used to expand a list into a sequence of rows.
7UNIONThis clause is used to combine the result of multiple queries.
8CALLThis clause is used to invoke a procedure deployed in the database.

Neo4j CQL Functions

Following are the frequently used Neo4j CQL Functions −

Sr.NoCQL FunctionsUsage
1StringThey are used to work with String literals.
2AggregationThey are used to perform some aggregation operations on CQL Query results.
3RelationshipThey are used to get details of relationships such as start node, end node, etc.

We will discuss all Neo4j CQL commands, clauses and functions syntax, usage, and examples in detail in the subsequent chapters.

Neo4j CQL Data Types

These data types are similar to the Java language. They are used to define the properties of a node or a relationship.

Neo4j CQL supports the following data types −

Sr.NoCQL Data TypeUsage
1BooleanIt is used to represent Boolean literals: true, false.
2byteIt is used to represent 8-bit integers.
3shortIt is used to represent 16-bit integers.
4intIt is used to represent 32-bit integers.
5longIt is used to represent 64-bit integers.
6floatIt is used to represent 32-bit floating-point numbers.
7doubleIt is used to represent 64-bit floating-point numbers.
8charIt is used to represent 16-bit characters.
9StringIt is used to represent Strings.

CQL Operators

Following is the list of operators supported by Neo4j Cypher Query language.

Sr.NoTypeOperators
1Mathematical+, -, *, /, %, ^
2Comparison+, <>, <, >, <=, >=
3BooleanAND, OR, XOR, NOT
4String+
5List+, IN, [X], [X…..Y]
6Regular Expression=-
7String matchingSTARTS WITH, ENDS WITH, CONSTRAINTS

Boolean Operators in Neo4j CQL

Neo4j supports the following Boolean operators to use in Neo4j CQL WHERE clause to support multiple conditions.

Sr.NoBoolean OperatorsDescription
1ANDIt is a Neo4j CQL keyword to support AND operation. It is like SQL AND operator.
2ORIt is a Neo4j CQL keyword to support OR operation. It is like SQL AND operator.
3NOTIt is a Neo4j CQL keyword to support NOT operation. It is like SQL AND operator.
4XORIt is a Neo4j CQL keyword to support XOR operation. It is like SQL AND operator.

Comparison Operators in Neo4j CQL

Neo4j supports the following Comparison operators to use in Neo4j CQL WHERE clause to support conditions.

Sr.NoBoolean OperatorsDescription
1=It is a Neo4j CQL “Equal To” operator.
2< >It is a Neo4j CQL “Not Equal To” operator.
3It is a Neo4j CQL “Less Than” operator.
4It is a Neo4j CQL “Greater Than” operator.
5<=It is a Neo4j CQL “Less Than Or Equal To” operator.
6> =It is a Neo4j CQL “Greater Than Or Equal To” operator.

Leave a Reply