FILTER statement¶
The FILTER statement selects a subset of the data produced by the preceding statement. The argument of the FILTER statement is a boolean expression that determines whether the data should be passed to the next statement.
Syntax¶
- filter_statement
filter_statement ::=
'FILTER' 'WHERE'? search_condition
Parameters¶
search_condition¶
A boolean expression to be applied to the data produced by the preceding statement. For more information, see WHERE clause.
If the data satisfies the search_condition, it is passed to the next statement; otherwise, it is discarded.
Examples¶
The following examples are based on the example dataset.
Filter with a WHERE clause¶
FOR i IN LIST[1, 2, 3] FILTER WHERE i>2 RETURN i
Return:
+---+
| i |
+---+
| 3 |
+---+
Filter without a WHERE clause¶
FOR i IN LIST[1, 2, 3] FILTER i>2 RETURN i
Return:
+---+
| i |
+---+
| 3 |
+---+
Filter the result of a MATCH statement¶
'''
USE movie
MATCH (v:Person) FILTER v.birthDate > date("1990-01-20")
RETURN v ORDER BY v.birthDate LIMIT 3
'''
Return:
+-------------------------------------------------------------------------------------+
| v |
+-------------------------------------------------------------------------------------+
| (288334086727008257@Actor:Person{birthDate:1990-01-30,id:9641,name:"Jake Thomas"}) |
| (288374798722007041@Actor:Person{birthDate:1990-04-15,id:10990,name:"Emma Watson"}) |
| (288488795743977473@Actor:Person{birthDate:1990-07-04,id:5847,name:"David Kross"}) |
+-------------------------------------------------------------------------------------+
Filter the result of a procedural call¶
'''
CALL show_graphs() YIELD name
FILTER name = "movie"
RETURN *
'''
Return:
+-------+
| name |
+-------+
| movie |
+-------+
Filter before aggregation¶
'''
USE movie
MATCH (v@Actor) FILTER v.birthDate > date("1995-01-20")
RETURN count(v) AS count GROUP BY ()
'''
The preceding statement filters actors born after January 20, 1995, and then counts the number of actors.
Return:
+-------+
| count |
+-------+
| 3 |
+-------+
Filter after aggregation¶
Since aggregation value expressions are typically used in RETURN statements, to filter the returned results after aggregation, you must use the keyword NEXT to pass the results to the next statement. For more information, see Statement block.
'''
USE movie
MATCH (v@Actor)
RETURN count(v) AS count, v.birthDate AS birthDate GROUP BY birthDate
NEXT
USE movie
FILTER birthDate > date("1995-01-20")
RETURN count, birthDate
'''
The preceding statement first counts the number of actors grouped by their birth dates, and then filters the results to include only those with birth dates after January 20, 1995.
Return:
+-------+------------+
| count | birthDate |
+-------+------------+
| 1 | 1997-07-17 |
| 1 | 1997-03-09 |
| 1 | 1998-01-12 |
+-------+------------+