---
# System prepended metadata

title: OpenCypher

---


# OpenCypher
## Understanding mandetory match 

The major difference between `MATCH` and `MANDATORY MATCH` is that in `MANDATORY MATCH` query is supposed to be failed and raise an error while in `MATCH` it should return `zero` records if nothing is found. 

According the the orignal documentation provided by the OpenCypher the objective of the `MANDATORY MATCH` is to understand the reason for query failure as the error that is supoosed to be raised should contain the information about itself. Since the only reason when `MANDATORY MATCH` will fail according the specification is when data defined in `MANDATORY MATCH` is not found therefore, the error code or error discripiton should contain information about the clause that is causing it to be failed. In other words, It should explain which cluase is return zero results. 

## What is a failed query:
A failed query is a query that can not be executd on the server because of syntex error or object that it is refering to doesnot exists. For example if table does not exists a SQL query will give an error but it will not give an error if a record in the relation/table does not exists. 

## Problems with this method: 
Theoratically this idea might be very useful however, looking into practicle side, I believe it will not be as useful as it orignally has been preceived. The one and only reason to explain this is raising error. Consider a java application, a python webpage using the query with `MANDATORY MATCH` and it raises error. How the diver will behave. It will raise an error and application will crash. Since crashing of an application is not in the faovour of anyone therefore, no one will use this cluase and let the application be crashed. Instead they will use ohter methods such as counts (more have been given in the following sections) to avoid application crash. Futhermore, I am unable from any of the SQL query that perform similar. For example, Given a join query where three tables are joined with eachother, a query does not tell which table has zero rows but it overall returns zero rows. It also furthermore doesn't return error or fail. 

## Alternatives: 

I have advocated in above section that there are structual and applied problems with the `MANDATORY MATCH` concept. Therefore, a question raises are there any alternatives exists? The answer is yes, there are few alternatives that can be used to improve simple `MATCH` cluase. 

One of the objective for `MANDATORY MATCH` is to raise error when nothing is find. In my view there is no need for a general error because if cursor is return `0` rows it means nothing have been found in the query. Therefore, user don't need to rewrite or rerun the query to get results again. This can be implemented at drivers lavel which can also contains information that how many record for each match clase were identified. 

Another way can be return result as JSON object instead of table. PG can return result as JSON using functions `json_build_object` and `json_agg` More information can be found into section 9.16 of PG manual. (valid up to PG15). Similar to this we can return a JSON object containing containing stats and array of results. In this way the `MATCH` will perform the same way as `MANDATORY MATCH`. 
 



