UltipaDocs
Products
Solutions
Resources
Company
Start Free Trial
UltipaDocs
Start Free Trial
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Questioned Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Open Graph
    • Closed Graph
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • SET
    • REMOVE
    • DELETE
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Element Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Null Functions
    • Utility Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
    • CASE
    • LET Value Expression
    • Value Query Expression
    • List Comprehension
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Backup and Restore
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Expressions

CASE

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:

  • Simple CASE
  • General CASE

Example Graph

GQL
INSERT (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)

Simple CASE

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

  • The <expr> is evaluated once, then compared for equality against each WHEN value in order.
  • The first match returns the corresponding THEN value.
  • If no match is found, returns the ELSE value. If ELSE is omitted, null is returned.
  • Only equality comparison is supported. For conditions involving operators (<, >, IS NULL, etc.), use General CASE.
GQL
MATCH (n:Paper WHERE n.score > 6)
RETURN CASE count(n) WHEN 3 THEN "Y" ELSE "N" END AS result

Result: "N"

GQL
MATCH (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.titlen.scorescoreLevel
Efficient Graph Search6Low
Optimizing Queries9High
Path Patterns7Medium

General CASE

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

  • Each <condition> is a boolean expression evaluated sequentially.
  • The first condition that evaluates to true returns the corresponding THEN value.
  • If no condition is true, returns the ELSE value. If ELSE is omitted, null is returned.
GQL
MATCH (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.titlenote
Optimizing QueriesPublisher N/A
Efficient Graph Search-1
Path PatternsZack
GQL
MATCH (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.titlen.scorescoreLevel
Efficient Graph Search6Low
Optimizing Queries9High
Path Patterns7Medium