The CASE expression is a conditional expression that allows you to evaluate one or more conditions and return different results based on those conditions.
Syntax<case expression> ::= <simple case> | <general case>
GQL supports two forms of the CASE expression:

GQLINSERT (p1:Paper {_id:'P1', title:'Efficient Graph Search', score:6, author:'Alex', publisher:'PulsePress'}), (p2:Paper {_id:'P2', title:'Optimizing Queries', score:9, author:'Alex'}), (p3:Paper {_id:'P3', title:'Path Patterns', score:7, author:'Zack', publisher:'BrightLeaf'}), (p1)-[:Cites {weight:2}]->(p2), (p2)-[:Cites {weight:1}]->(p3)
The simple CASE expression evaluates a single expression against multiple possible values, returning the result associated with the first matching value.
Syntax<simple case> ::= "CASE" <expr> { "WHEN" <value> "THEN" <expr> }... [ "ELSE" <expr> ] "END"
Details
<expr> is evaluated once, then compared for equality against each WHEN value in order.THEN value.ELSE value. If ELSE is omitted, null is returned.<, >, IS NULL, etc.), use General CASE.GQLMATCH (n:Paper WHERE n.score > 6) RETURN CASE count(n) WHEN 3 THEN "Y" ELSE "N" END AS result
Result: "N"
GQLMATCH (n:Paper) RETURN n.title, n.score, CASE n.score WHEN 6 THEN "Low" WHEN 7 THEN "Medium" WHEN 8 THEN "Medium" ELSE "High" END AS scoreLevel
Result:
| n.title | n.score | scoreLevel |
|---|---|---|
| Efficient Graph Search | 6 | Low |
| Optimizing Queries | 9 | High |
| Path Patterns | 7 | Medium |
The general CASE expression evaluates multiple conditions, returning the result associated with the first condition that evaluates to true.
Syntax<general case> ::= "CASE" { "WHEN" <condition> "THEN" <expr> }... [ "ELSE" <expr> ] "END"
Details
<condition> is a boolean expression evaluated sequentially.THEN value.ELSE value. If ELSE is omitted, null is returned.GQLMATCH (n:Paper) RETURN n.title, CASE WHEN n.publisher IS NULL THEN "Publisher N/A" WHEN n.score < 7 THEN -1 ELSE n.author END AS note
Result:
| n.title | note |
|---|---|
| Optimizing Queries | Publisher N/A |
| Efficient Graph Search | -1 |
| Path Patterns | Zack |
GQLMATCH (n:Paper) RETURN n.title, n.score, CASE WHEN n.score < 7 THEN "Low" WHEN n.score <= 8 THEN "Medium" ELSE "High" END AS scoreLevel
Result:
| n.title | n.score | scoreLevel |
|---|---|---|
| Efficient Graph Search | 6 | Low |
| Optimizing Queries | 9 | High |
| Path Patterns | 7 | Medium |