UltipaDocs
Try Playground
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Closed Graph
    • Open Graph
    • Graph Sharding and Storage
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • 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
    • Scalar Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Querying

FILTER

Overview

The FILTER statement allows you to discard records in the intermediate result table that do not satisfy the specified conditions.

Syntax
<filter statement> ::= "FILTER" [ "WHERE" ] <search condition>

The FILTER and FILTER WHERE behave the same way. The use of WHERE in this context is often a matter of style or readability. For example:

GQL
MATCH (n:User)
FILTER n.age > 25
RETURN n

is functionally identical to:

GQL
MATCH (n:User)
FILTER WHERE n.age > 25
RETURN n

In both cases, the FILTER statement returns nodes where the user's age is greater than 25.

FILTER vs. MATCH WHERE

The FILTER statement and the WHERE clause are both used to apply filtering conditions in queries, but they differ in when and how they are evaluated.

The WHERE is a clause that can only be used with the MATCH statement. It can appear inside node or edge patterns, or directly after the list of path patterns, and is evaluated as part of the graph pattern matching process.

GQL
MATCH (n:User)
WHERE n.age > 25
RETURN n

FILTER is a standalone statement that offers greater flexibility and can be used wherever needed within a query.

GQL
MATCH (n:User)
FILTER n.age > 25
RETURN n

Example Graph

CREATE GRAPH myGraph { 
  NODE User ({name string}),
  NODE Club ({since uint32}),
  EDGE Follows ()-[{createdOn date}]->(),
  EDGE Joins ()-[{memberNo uint32}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]

Simple Filtering

GQL
MATCH (c:Club)
FILTER c._id = "C01"
RETURN c

Result: c

_id_uuidschema
values
C01Sys-genClub{since: 2005}
GQL
FOR item IN [1,2,3] 
FILTER item > 1
RETURN item

Result: item

item
2
3

Filtering with Cartesian Product

This query returns users who follow Brainy and are also members of C02:

GQL
MATCH (u1:User)-[:Follows]->(:User {name: "Brainy"})
MATCH (u2:User)-({_id: "C02"})
FILTER u1 = u2
RETURN u1

Result: u1

_id_uuidschema
values
U04Sys-genUser{name: "mochaeach"}

Note that the Cartesian product is formed between u1 and u2, as they are produced by different MATCH statements, before the FILTER statement is applied to perform the filtering.