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
    • Cheapest Paths
    • K-Hop Traversal
    • Graph Patterns
    • Overview
    • Open Graphs
    • Closed Graphs
    • Graphs with Edge ID
    • Graph Types
    • Constraints
    • Projections
    • Storage Maintenance
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • MERGE
    • SET
    • REMOVE
    • DELETE
    • FOREACH
    • 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
    • Database Functions
  • Operators
  • Predicates
    • CASE
    • LET Value Expression
    • Value Query Expression
    • List Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Execution Plan
  • Backup and Restore
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Naming Conventions
    • 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 search conditions.

Syntax
<filter statement> ::= "FILTER" <search condition>

FILTER vs. WHERE

WHERE is a clause bound to the MATCH statement, which is evaluated during pattern matching. FILTER is a standalone statement that is evaluated after the preceding statement produces its result.

In many cases they produce the same result:

GQL
-- Using WHERE (evaluated during MATCH)
MATCH (n:User) WHERE n.age > 25
RETURN n

-- Using FILTER (evaluated after MATCH)
MATCH (n:User)
FILTER n.age > 25
RETURN n

The difference matters with OPTIONAL MATCH. WHERE is applied before the optional logic, while FILTER is applied after — see OPTIONAL MATCH for details.

FILTER also works with non-MATCH statements like FOR:

GQL
FOR item IN [20, 34, 56]
FILTER item > 30
RETURN item

Replacing FILTER with WHERE in this query will cause syntax error.

Example Graph

GQL
INSERT (rowlock:User {_id: 'U01', name: 'rowlock'}),
       (brainy:User {_id: 'U02', name: 'Brainy'}),
       (purplechalk:User {_id: 'U03', name: 'purplechalk'}),
       (mochaeach:User {_id: 'U04', name: 'mochaeach'}),
       (lionbower:User {_id: 'U05', name: 'lionbower'}),
       (c01:Club {_id: 'C01', since: 2005}),
       (c02:Club {_id: 'C02', since: 2005}),
       (rowlock)-[:Follows {createdOn: '2024-01-05'}]->(brainy),
       (mochaeach)-[:Follows {createdOn: '2024-02-10'}]->(brainy),
       (brainy)-[:Follows {createdOn: '2024-02-01'}]->(purplechalk),
       (lionbower)-[:Follows {createdOn: '2024-05-03'}]->(purplechalk),
       (brainy)-[:Joins {memberNo: 1}]->(c01),
       (lionbower)-[:Joins {memberNo: 2}]->(c01),
       (mochaeach)-[:Joins {memberNo: 9}]->(c02)

Simple Filtering

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

Result:

JSON
[
  {"id": "C01", "labels": ["Club"], "properties": {"since": 2005}}
]

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:

JSON
[
  {"id": "U04", "labels": ["User"], "properties": {"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.