How to merge these two queries into one?

Query 1:
Match (n:Product) where n.brand = 'Apple' return n

Query 2:
Match (n:Product) -[r:hasBrand]->(m:Brand {name:'Apple'}) return n

Is possible merge these two into 1?

By merge do you merge do you mean where Product.brand = Apple and = Apple?

If not then please lete us know the meaning of merge

Yes. My Product has a 'brand' property, and also it has a 'hasBrand' relationship to Brand, which isn't good design. I want to find all products which has a brand associated, which could be through either the property n.brand, or the relationship [:hasBrand], and that's why I wrote 2 separate queries to get two sets of results, and then combine together.

My question, how to write this as one query to achieve the same effect?

Are you saying you have set of Product which are associated with any Brand

  1. Has property as brand
  2. Has a relationship with Brand

and both are disjoint sets?

This should do the trick:

MATCH (n:Product { brand = 'Apple' })
OPTIONAL MATCH (n) -[r:hasBrand]-> ( m:Brand {name:'Apple'})
return n

That's great and thank you!

@sebastian A similar question:

How to query 'products which has the 'brand' property but doesn't have a hasBrand relationship'?

... WHERE r IS NULL ?!

What exactly are you trying to do there ?
Are you trying to just find the brand Apple ?

Anyways you can combine both by using OPTIONAL MATCH attached to the 2nd query.

All the best :grinning: