Overview
The WHERE
clause can be used to apply filters within a graph pattern in the MATCH
statement. It supports a host of search conditions, and multiple conditions can be combined into a logical expression using operators like AND
, OR
, NOT
, and XOR
.
<where clause> ::= "WHERE" <search condition>
<search condition> ::= <boolean value expression>
GQL supports three WHERE
clauses:
- Element pattern
WHERE
clause - Parenthesized path
WHERE
clause - Graph pattern
WHERE
clause
The element pattern WHERE
clause and parenthesized path pattern WHERE
clause can be viewed as prefilters applied before selection; while the graph pattern WHERE
clause as postfilters applied after selection.
Element Pattern WHERE Clause
You can use the WHERE
clause within an element pattern as a predicate to apply conditions.
The element pattern WHERE
clause offers more search conditions compared to the element property specification, which only allows applying joint equalities based on key-value pairs for properties.
In this query, the WHERE
clause is used within a node pattern to filter nodes labeled Card
where the property level
is greater than 3:
MATCH p = (c:Card WHERE c.level > 3)-[:Transfers]->()
RETURN p
In this query, the WHERE
clause is used within an edge pattern to filter edges that have properties amount
and currency
, and ensure their values meet specified conditions:
MATCH p = (:Card)-[t WHERE t.amount > 10000 AND t.currency = 'USD']->()
RETURN p
This query throws a syntax error because amount
is used as if it were a variable, but it has not been declared. To specify a property in the WHERE
clause, you must use the dot operator .
to reference it, like (n:Card WHERE n.amount > 100)
.
MATCH p = (:Card WHERE amount > 100)-[]->()
RETURN p
This query throws a syntax error because you cannot use property specification and WHERE
clause together in an element pattern. Instead, you may write (n:Paper WHERE n.author = "Alex" AND n.score > 5)
.
MATCH (n:Paper {author: "Alex"} WHERE n.score > 5)
RETURN n
Parenthesized Path Pattern WHERE Clause
You can use the WHERE
clause within a parenthesized path pattern as a predicate to apply conditions.
In this query, the WHERE
clause is used in a parenthesized path pattern to filter each edge contained in the quantified paths:
MATCH p = ((:Card)-[t:Transfers]->(:Card) WHERE t.amount > 200){1,3}
RETURN p
Using the
WHERE
clause in a quantified path pattern is not yet supported.
Graph Pattern WHERE Clause
You can use the WHERE
clause as a predicate to the graph pattern to apply conditions.
In this query, the WHERE
clause is used to filter people, each of them lives in and is from the same city:
MATCH (p:Person)-[:LivesIn]->(c1:City),
(p)-[:IsFrom]->(c2:City)
WHERE c1 = c2
RETURN p