WHERE¶
The WHERE clause is used to filter the results of MATCH patterns.
Basic Filtering¶
Property Comparison¶
Multiple Conditions¶
OR Conditions¶
Comparison Operators¶
-- Equality
WHERE u.name = 'Alice'
-- Inequality
WHERE u.age <> 30
-- Numeric comparison
WHERE u.age > 25
WHERE u.age >= 18
WHERE u.age < 65
WHERE u.age <= 100
-- String comparison
WHERE u.name > 'A' -- Alphabetical
String Matching¶
Exact Match¶
Case-Insensitive¶
Contains¶
Starts With¶
Ends With¶
Regular Expressions¶
NULL Checks¶
IS NULL¶
IS NOT NULL¶
List Operations¶
IN Operator¶
List Membership¶
Range Checks¶
-- Age between 25 and 35
WHERE u.age >= 25 AND u.age <= 35
-- Using IN for discrete values
WHERE u.age IN [25, 26, 27, 28, 29, 30]
Pattern Filters¶
Relationship Existence¶
NOT Pattern¶
Path Existence¶
Boolean Logic¶
NOT¶
Complex Expressions¶
Property Existence¶
-- Check if property exists
WHERE EXISTS(u.email)
-- Check if property doesn't exist
WHERE NOT EXISTS(u.phone)
Using Parameters¶
Label Checking¶
Filtering Relationships¶
Common Patterns¶
Active Users¶
Users with Friends¶
Exclude Self-Loops¶
Time Range¶
MATCH (p:Post)
WHERE p.created >= datetime('2024-01-01')
AND p.created < datetime('2024-02-01')
RETURN p
Performance Tips¶
-
Use Indexed Properties
-
Most Selective Filters First
-
Avoid Property Functions in Filters
-
Use Parameters
Next Steps¶
- Match patterns with MATCH
- Select output with RETURN
- Sort results with ORDER BY
- Read Cypher Introduction