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: Subtracts matches from the 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.
- SERVICE: Queries remote data at a SPARQL endpoint.
- UNION: Includes results from either of two graph patterns. Solutions to both sides of the union are included in the results.
- 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 Syntax
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 Syntax
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 comparison, conditional, and testing operators and functions in filters, see Comparison Operators and Functions, Conditional Functions and Negation, and Functions Used for Testing Values.
MINUS, OPTIONAL, UNION Syntax
Use the following syntax when incorporating MINUS, OPTIONAL, or UNION statements in WHERE clauses:
KEYWORD{ triple_patterns }
SERVICE Syntax
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. The query returns an abstract about Keanu Reeves:
SELECT * WHERE { SERVICE <http://dbpedia.org/sparql> { <http://dbpedia.org/resource/Keanu_Reeves> <http://dbpedia.org/ontology/abstract> ?o. FILTER LANGMATCHES(LANG(?o), "en") } }
o ------------------------------------------------- Keanu Charles Reeves (/keɪˈɑːnuː/ kay-AH-noo; born September 2, 1964) is a Canadian actor...
VALUES Syntax
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.
SELECT DISTINCT ?event ?category FROM <tickit> WHERE { ?s <eventid> ?eventid . ?eventid <dateid> ?dateid . ?dateid <caldate> ?date . ?eventid <eventname> ?event . ?eventid <catid> ?cat . ?cat <catname> ?category . ?dateid <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.
SELECT ?event ?tickets ((?total_paid - ?commission_paid) as ?profit) FROM <tickit> WHERE { SELECT ?event (sum(?qty) as ?tickets) (sum(?comm) as ?commission_paid) (sum(?price) as ?total_paid) WHERE { ?sales <qtysold> ?qty . ?sales <eventid> ?eventid . ?eventid <eventname> ?event . ?sales <commission> ?comm . ?sales <pricepaid> ?price . } GROUP BY ?event } ORDER BY desc(?profit) LIMIT 100
event | tickets | profit -----------------+---------+--------------- Mamma Mia! | 3658 | 965135.900000 Spring Awakening | 3025 | 826926.750000 The Country Girl | 2871 | 773978.550000 Macbeth | 2733 | 733193.000000 Jersey Boys | 2781 | 690095.450000 Legally Blonde | 2272 | 683895.550000 Chicago | 2535 | 672344.050000 ... 100 rows