The OPTIONAL clause

Missing values and the OPTIONAL clause

As we’ve seen, when there is more than one selection pattern in the WHERE section, there is an implicit AND between the statement, such that only patterns that match all statements will be returned by the query. For example, in the last exercise, there were three selection patterns:

#Star Wars films

SELECT ?item  ?itemLabel ?directorLabel ?cost
  ?item wdt:P179 wd:Q22092344.       # item is part of the series Star Wars (film series)
  ?item wdt:P57 ?director.           # item’s director property’s value is collected by the director variable
  OPTIONAL {?item wdt:P2130 ?cost.}  # item's cost property's value is collected by the cost variable
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }

The statements on line 6, 7, and 8 have an AND relation between them. Therefore, an item will be considered a match only if it has a P179 property (part of a series) with a value of Q22092344 (Star Wars film series), as well as a P57 (director) property and a P2130 (cost) property. If an item doesn’t match one of these statements (e.g., doesn’t have the property cost) it is ignored by the query.  

SPARQL is a pattern-matching query language. SPARQL queries will only return data when the pattern in the WHERE section exactly matches the pattern in the data you’re querying. But many datasets have missing values, and data is only a match for the query if there is valid data in each piece of the statement declared within WHERE. This means SPARQL will not return an item that is missing property name or value requested in WHERE.

The OPTIONAL keyword within the WHERE section denotes optional patterns you’d like to match in the data. OPTIONAL allows to search for data that may or may not be there.

Run the query again with cost as an optional pattern.

Skip to content