WHERE Clause
The WHERE clause defines the query patterns to look for in the specified graphs or data sets. WHERE clauses can include graph and triple templates, subqueries, and the following clauses:
- BIND: Assigns the results of an expression to a new variable.
- FILTER: Applies boolean conditions or tests to constrain results and filter out values that do not meet the specified conditions.
- MINUS, OPTIONAL, UNION: MINUS subtracts matches from the query result based on the evaluation of the pattern that you specify. OPTIONAL tries to match a graph pattern but does not fail to return results is the optional match fails. UNION Includes results from either of two graph patterns with solutions to both sides of the union are included in the results.
- SERVICE: Queries remote data at a SPARQL endpoint.
- VALUES: Enables users to include data in a graph pattern to filter results on more specific requirements. The data is joined with the results of the query evaluation.
BIND
Use the following syntax when incorporating BIND in the WHERE clause:
BIND(expression AS ?variable)
Where expression evaluates to the values that you want to bind to the variable.
FILTER
Use the following syntax when incorporating filters in WHERE clauses:
FILTER(expression [ logical_operator expression ] [...])
Where expression is the condition to test for. You can also use the logical operators && (AND), || (OR), or ! (NOT) to combine filter expressions. For information about using logical operators and functions in filters, see Logical Functions.
MINUS, OPTIONAL, UNION
Use the following syntax when incorporating MINUS, OPTIONAL, or UNION statements in WHERE clauses:
KEYWORD { triple_patterns }
SERVICE
Use the following syntax when incorporating SERVICE statements in WHERE clauses. SERVICE statements have the same structure as named graph statements:
SERVICE{ <endpoint_URL> { triple_patterns } }
Where endpoint_URL is the URL for accessing the remote SPARQL endpoint, and triple_patterns define the patterns to look for in the remote data.
For example, the following query uses a SERVICE call to retrieve data from the DBpedia SPARQL endpoint.
SELECT * WHERE { SERVICE <http://dbpedia.org/sparql> { <http://dbpedia.org/resource/Keanu_Reeves> <http://dbpedia.org/ontology/abstract> ?o. FILTER LANGMATCHES(LANG(?o), "en") } }
The query returns an abstract about Keanu Reeves:
o ------------------------------------------------- Keanu Charles Reeves (/keɪˈɑːnuː/ kay-AH-noo; born September 2, 1964) is a Canadian actor...
VALUES
Use the following syntax when incorporating VALUES statements in WHERE clauses:
VALUES ?variable { value_for_variable [ another_possible_value ] [...] }
Where ?variable is the node that you want to find values for, and value_for_variable is the value to look for.
To further constrain the results by specifying multiple variables and possible values, use the following syntax:
VALUES ( ?variable1 ?variable2 [...] ) { ( [UNDEF] | value_for_variable1 [UNDEF] | value_for_variable2 [...]) ( [UNDEF] | another_value_for_variable1 [UNDEF] | another_value_for_variable2 [...]) ( [ ... ] ) }
The UNDEF keyword acts as a wildcard instead of a specific value. For example, the following VALUES clause states: "Include matches when ?b =n
regardless of the value for ?a, and include matches when ?a=x
regardless of the value for ?b."
VALUES ( ?a ?b ) { ( UNDEF "n" ) ( "x" UNDEF ) }
WHERE Clause Examples
The WHERE clause in the example query below uses triple patterns and a filter to query the sample Tickit data set and return a list of all of the musicals that occur on a holiday.
PREFIX tickit: <http://anzograph.com/tickit/> SELECT DISTINCT ?event ?category FROM <http://anzograph.com/tickit> WHERE { ?s tickit:eventid ?eventid . ?eventid tickit:dateid ?dateid . ?dateid tickit:caldate ?date . ?eventid tickit:eventname ?event . ?eventid tickit:catid ?cat . ?cat tickit:catname ?category . ?dateid tickit:holiday ?holiday . FILTER(?holiday=true && ?category="Musicals") } ORDER BY ?event
event | category -------------------------+---------- A Catered Affair | Musicals Chicago | Musicals Curtains | Musicals Dirty Dancing | Musicals Folies Bergere | Musicals Grease | Musicals High Society | Musicals Legally Blonde | Musicals Mamma Mia! | Musicals Oliver! | Musicals Phantom of the Opera | Musicals Spamalot | Musicals The King and I | Musicals The Phantom of the Opera | Musicals West Side Story | Musicals 15 rows
The WHERE clause in the query below includes a subquery that joins sales and event data to return ticket, commission, and price paid information for each event. The top-level result clause uses the subquery results to subtract the commission paid from the total price paid to calculate the profit for each event.
PREFIX tickit: <http://anzograph.com/tickit/> SELECT ?event ?tickets ((?total_paid - ?commission_paid) as ?profit) FROM <http://anzograph.com/tickit> WHERE { SELECT ?event (sum(?qty) as ?tickets) (sum(?comm) as ?commission_paid) (sum(?price) as ?total_paid) WHERE { ?sales tickit:qtysold ?qty . ?sales tickit:eventid ?eventid . ?eventid tickit:eventname ?event . ?sales tickit:commission ?comm . ?sales tickit:pricepaid ?price . } GROUP BY ?event } ORDER BY desc(?profit) LIMIT 10
event | tickets | profit -----------------+---------+-------- Mamma Mia! | 3658 | 965136 Spring Awakening | 3025 | 826927 The Country Girl | 2871 | 773979 Macbeth | 2733 | 733193 Jersey Boys | 2781 | 690095 Legally Blonde | 2272 | 683896 Chicago | 2535 | 672344 Spamalot | 2199 | 607161 Hedda Gabler | 1891 | 561865 Thurgood | 1894 | 543895 10 rows