Neo4j CQL - UNION ALL




UNION ALL Clause

It combines and returns all rows from two set of results into a single set of results. It returns duplicate rows also from two nodes.

Restriction:

Result column types and names from two set of results have to match that means column names should be same and column's data types should be same.

UNION ALL Clause syntax:

<MATCH Command1>
UNION ALL
<MATCH Command2>

Syntax Description

S.No. Syntax Element Description
1. <MATCH Command1> It is CQL MATCH command one to be used by UNION clause.
2. <MATCH Command2> It is CQL MATCH command two to be used by UNION clause.
3. UNION ALL It is a Neo4j CQL keyword for UNION ALL clause.

Note:-

If both queries does not return same column names and data types, then it throws an error.

In this chapter, we will take a Banking application nodes : CreditCard and DebitCard to explain UNION clause

CreditCard Node data

  • Open Neo4j Data Browser

  • Neo4j CQL Tutorial

    It is Neo4j Data Browser Homepage

  • Type the below command at dollar prompt in Data Browser.

MATCH (cc:CreditCard) RETURN cc

Click on "Download CSV" to see the results

Neo4j CQL Tutorial

DebitCard Node data

  • Open Neo4j Data Browser

  • Neo4j CQL Tutorial

    It is Neo4j Data Browser Homepage

  • Type the below command at dollar prompt in Data Browser.

MATCH (dc:DebitCard) RETURN dc

Click on "Download CSV" to see the results

Neo4j CQL Tutorial

We will use this data to explain the usage of Neo4j CQL UNION with examples

Example1:-

This Example demonstrates what will happen if both queries of UNION clause does have same names or same data types to their columns.

Steps to follow:

  • Open Neo4j Data Browser

  • Neo4j CQL Tutorial

    It is Neo4j Data Browser Homepage

  • Type the below command at dollar prompt in Data Browser.

MATCH (cc:CreditCard)
RETURN cc.id as id,cc.number as number,cc.name as name,
	   cc.valid_from as valid_from,cc.valid_to as valid_to
UNION ALL
MATCH (dc:DebitCard)
RETURN dc.id as id,dc.number as number,dc.name as name,
       dc.valid_from as valid_from,dc.valid_to as valid_to
  • Click on Execute button and see the success message in the Data Browser.

  • Neo4j CQL Tutorial

    Here we can observe this command returns 10 rows with duplicate rows because UNION ALL clause does not filter them. If we use UNION clause, it returns only 9 rows. Please refer UNION clause chapter to check this.


    Advertisements