FILTER – excluding results
So far we saw modifiers that operated at the SELECT clause or after the WHERE clause. Another useful modifier is FILTER, which can appear within the WHERE clause.
We will demonstrate filtering in two ways: the FILTER function which selects only the items that match the filter condition (i.e., excludes items that do not) and FILTER NOT EXISTS which is a clause that excludes results that match certain patterns.
The FILTER function excludes items based on the expression that appears within the function’s parenthesis: only those results for which the expression in the parenthesis is true are used.
Let’s say we wanted to edit the query about the 10 highest-grossing films that are part of a series, so that the query will return only those films that were released before the year 2000.
On line 8, we added a statement to collect the value of P577 property (publication date) with the ?date variable. On the next line we used FILTER to only include items whose publication date is before 01-01-2000.
Note that we put the criterion date (2000-01-01) in quotes and add “^^xsd:dateTime” after to signify that the format of this value is a date.
Another way of writing this query is using the function YEAR. This built-in function returns the year part of a date (or a dateTime value):
FILTER NOT EXISTS
Let’s say we wanted to edit the query about actresses nominated for more than three Academy awards, so that the query will return only those actresses that never received an Academy award.
We add the FILTER NOT EXISTS clause, in which we specify the patterns that should NOT be matched, namely that the actress has a P166 property (award received) that has a value that is an instance (P31) of Academy Awards (Q19020):
In other words, the query excludes items that have statements that match the pattern given within the FILTER NOT EXISTS clause.